sys.exit() versus exit() to terminate a standalone script

6649
3
Jump to solution
01-21-2020 09:24 AM
JoeBorgione
MVP Esteemed Contributor

I'm a little confused with the difference between these two approaches and could use some clarification.  I have a stand alone python script in which all my geo-processes are enclosed in try/except blocks which is my standard operating procedure.  However, in this particular script, if any exceptions are tossed, I want the script to abort.  It will be run as a scheduled task.

sys — System-specific parameters and functions — Python 3.8.1 documentation  mentions: 

sys.exit([arg])https://docs.python.org/3/library/sys.html#sys.exit

Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level....

...

Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

If I try to use sys.exit in a console  I get scolded with:

sys.exit()
An exception has occurred, use %tb to see the full traceback.

SystemExit

C:\EnvClones\arcgispro-py3-clone243\lib\site-packages\IPython\core\interactiveshell.py:3327: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

So using exit() is the clear favorite there.  Is sys.exit() preferred in a stand alone script?

That should just about do it....
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
3 Replies
JoshuaBixby
MVP Esteemed Contributor
JoeBorgione
MVP Esteemed Contributor

Thanks- read that too... hence my confusion...

However, reading through it again, the conclusions section states:

Use sys.exit() in scripts, or raise SystemExit() if you prefer.

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Yeah, exit() is more for convenience when working interactively in the interpreter, at least that is my very high-level summary.

0 Kudos