<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic sys.exit() versus exit() to terminate a standalone script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166732#M12853</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm a little confused with the difference between these two approaches and could use some clarification.&amp;nbsp; I have a stand alone python script in which all my geo-processes are enclosed in try/except blocks which is&amp;nbsp;my standard operating procedure.&amp;nbsp; However, in this particular script, if any exceptions are tossed, I want the script to abort.&amp;nbsp; It will be run as a scheduled task.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://docs.python.org/3/library/sys.html" title="https://docs.python.org/3/library/sys.html" rel="nofollow noopener noreferrer" target="_blank"&gt;sys — System-specific parameters and functions — Python 3.8.1 documentation&lt;/A&gt;&amp;nbsp; mentions:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;CODE class=""&gt;sys.&lt;/CODE&gt;&lt;CODE class=""&gt;exit&lt;/CODE&gt;&lt;SPAN class="" style="font-size: larger;"&gt;(&lt;/SPAN&gt;&lt;SPAN class="" style="font-size: 1.3em;"&gt;[&lt;/SPAN&gt;arg&lt;SPAN class="" style="font-size: 1.3em;"&gt;]&lt;/SPAN&gt;&lt;SPAN class="" style="font-size: larger;"&gt;)&lt;/SPAN&gt;&lt;A href="https://docs.python.org/3/library/sys.html#sys.exit" style="color: #6363bb; text-decoration: none; font-size: 0.8em; padding: 0px 4px;" title="Permalink to this definition" rel="nofollow noopener noreferrer" target="_blank"&gt;https://docs.python.org/3/library/sys.html#sys.exit&lt;/A&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Exit from Python. This is implemented by raising the&amp;nbsp;&lt;A href="https://docs.python.org/3/library/exceptions.html#SystemExit" style="color: #6363bb; text-decoration: none;" title="SystemExit" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;CODE class=""&gt;&lt;SPAN class=""&gt;SystemExit&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/A&gt;&amp;nbsp;exception, so cleanup actions specified by finally clauses of&amp;nbsp;&lt;A href="https://docs.python.org/3/reference/compound_stmts.html#try" style="color: #6363bb; text-decoration: none;" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;CODE class=""&gt;&lt;SPAN class=""&gt;try&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/A&gt;&amp;nbsp;statements are honored, and it is possible to intercept the exit attempt at an outer level....&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;...&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Since&amp;nbsp;&lt;A href="https://docs.python.org/3/library/constants.html#exit" style="color: #6363bb; text-decoration: none;" title="exit" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;CODE class=""&gt;&lt;SPAN class=""&gt;exit()&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/A&gt;&amp;nbsp;ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I try to use sys.exit in a console&amp;nbsp; I get scolded with:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;sys&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;exit&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
An exception has occurred&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; use &lt;SPAN class="operator token"&gt;%&lt;/SPAN&gt;tb to see the full traceback&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;

SystemExit

C&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;\EnvClones\arcgispro&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;py3&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;clone243\lib\site&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;packages\IPython\core\interactiveshell&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;py&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;3327&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; UserWarning&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; To exit&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; use &lt;SPAN class="string token"&gt;'exit'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'quit'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;or&lt;/SPAN&gt; Ctrl&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;D&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;
  warn&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"To exit: use 'exit', 'quit', or Ctrl-D."&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; stacklevel&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So using exit() is the clear favorite there.&amp;nbsp; Is sys.exit() preferred in a stand alone script?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 08:41:44 GMT</pubDate>
    <dc:creator>JoeBorgione</dc:creator>
    <dc:date>2021-12-11T08:41:44Z</dc:date>
    <item>
      <title>sys.exit() versus exit() to terminate a standalone script</title>
      <link>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166732#M12853</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm a little confused with the difference between these two approaches and could use some clarification.&amp;nbsp; I have a stand alone python script in which all my geo-processes are enclosed in try/except blocks which is&amp;nbsp;my standard operating procedure.&amp;nbsp; However, in this particular script, if any exceptions are tossed, I want the script to abort.&amp;nbsp; It will be run as a scheduled task.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://docs.python.org/3/library/sys.html" title="https://docs.python.org/3/library/sys.html" rel="nofollow noopener noreferrer" target="_blank"&gt;sys — System-specific parameters and functions — Python 3.8.1 documentation&lt;/A&gt;&amp;nbsp; mentions:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;CODE class=""&gt;sys.&lt;/CODE&gt;&lt;CODE class=""&gt;exit&lt;/CODE&gt;&lt;SPAN class="" style="font-size: larger;"&gt;(&lt;/SPAN&gt;&lt;SPAN class="" style="font-size: 1.3em;"&gt;[&lt;/SPAN&gt;arg&lt;SPAN class="" style="font-size: 1.3em;"&gt;]&lt;/SPAN&gt;&lt;SPAN class="" style="font-size: larger;"&gt;)&lt;/SPAN&gt;&lt;A href="https://docs.python.org/3/library/sys.html#sys.exit" style="color: #6363bb; text-decoration: none; font-size: 0.8em; padding: 0px 4px;" title="Permalink to this definition" rel="nofollow noopener noreferrer" target="_blank"&gt;https://docs.python.org/3/library/sys.html#sys.exit&lt;/A&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Exit from Python. This is implemented by raising the&amp;nbsp;&lt;A href="https://docs.python.org/3/library/exceptions.html#SystemExit" style="color: #6363bb; text-decoration: none;" title="SystemExit" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;CODE class=""&gt;&lt;SPAN class=""&gt;SystemExit&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/A&gt;&amp;nbsp;exception, so cleanup actions specified by finally clauses of&amp;nbsp;&lt;A href="https://docs.python.org/3/reference/compound_stmts.html#try" style="color: #6363bb; text-decoration: none;" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;CODE class=""&gt;&lt;SPAN class=""&gt;try&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/A&gt;&amp;nbsp;statements are honored, and it is possible to intercept the exit attempt at an outer level....&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;...&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Since&amp;nbsp;&lt;A href="https://docs.python.org/3/library/constants.html#exit" style="color: #6363bb; text-decoration: none;" title="exit" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;CODE class=""&gt;&lt;SPAN class=""&gt;exit()&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/A&gt;&amp;nbsp;ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I try to use sys.exit in a console&amp;nbsp; I get scolded with:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;sys&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;exit&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
An exception has occurred&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; use &lt;SPAN class="operator token"&gt;%&lt;/SPAN&gt;tb to see the full traceback&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;

SystemExit

C&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;\EnvClones\arcgispro&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;py3&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;clone243\lib\site&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;packages\IPython\core\interactiveshell&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;py&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;3327&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; UserWarning&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; To exit&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt; use &lt;SPAN class="string token"&gt;'exit'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'quit'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;or&lt;/SPAN&gt; Ctrl&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;D&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;
  warn&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"To exit: use 'exit', 'quit', or Ctrl-D."&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; stacklevel&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So using exit() is the clear favorite there.&amp;nbsp; Is sys.exit() preferred in a stand alone script?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:41:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166732#M12853</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-12-11T08:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: sys.exit() versus exit() to terminate a standalone script</title>
      <link>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166733#M12854</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Some discussion on StackOverflow:&amp;nbsp; &lt;A class="link-titled" href="https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used" title="https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used"&gt;Python exit commands - why so many and when should each be used? - Stack Overflow&lt;/A&gt;&amp;nbsp; and &lt;A class="link-titled" href="https://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python" title="https://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python"&gt;Difference between exit() and sys.exit() in Python - Stack Overflow&lt;/A&gt; .&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jan 2020 17:35:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166733#M12854</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-01-21T17:35:56Z</dc:date>
    </item>
    <item>
      <title>Re: sys.exit() versus exit() to terminate a standalone script</title>
      <link>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166734#M12855</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks- read that too... hence my confusion...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, reading through it again, the conclusions section states:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #242729; background-color: #ffffff;"&gt;Use&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;CODE style="color: #242729; background-color: #eff0f1; border: 0px; padding: 1px 5px;"&gt;sys.exit()&lt;/CODE&gt;&lt;SPAN style="color: #242729; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;in scripts, or&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;CODE style="color: #242729; background-color: #eff0f1; border: 0px; padding: 1px 5px;"&gt;raise SystemExit()&lt;/CODE&gt;&lt;SPAN style="color: #242729; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;if you prefer.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jan 2020 17:50:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166734#M12855</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-01-21T17:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: sys.exit() versus exit() to terminate a standalone script</title>
      <link>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166735#M12856</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yeah, exit() is more for convenience when working interactively in the interpreter, at least that is my very high-level summary.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jan 2020 20:06:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sys-exit-versus-exit-to-terminate-a-standalone/m-p/166735#M12856</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-01-21T20:06:00Z</dc:date>
    </item>
  </channel>
</rss>

