Python Exit Commands - Why So Many and When Should Each Be Used

Python exit commands - why so many and when should each be used?

The functions* quit(), exit(), and sys.exit() function in the same way: they raise the SystemExit exception. So there is no real difference, except that sys.exit() is always available but exit() and quit() are only available if the site module is imported (docs).

The os._exit() function is special, it exits immediately without calling any cleanup functions (it doesn't flush buffers, for example). This is designed for highly specialized use cases... basically, only in the child after an os.fork() call.

Conclusion

  • Use exit() or quit() in the REPL.

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

  • Use os._exit() for child processes to exit after a call to os.fork().

All of these can be called without arguments, or you can specify the exit status, e.g., exit(1) or raise SystemExit(1) to exit with status 1. Note that portable programs are limited to exit status codes in the range 0-255, if you raise SystemExit(256) on many systems this will get truncated and your process will actually exit with status 0.

Footnotes

* Actually, quit() and exit() are callable instance objects, but I think it's okay to call them functions.

Python function exit does not work - why?

To answer your question as directly as possible,

Why doesn't exit() stop the script after the first iteration?

Because a bare except will catch BaseException and Exception, and since exit() raises a SystemExit, which is a BaseException subclass, your code will swallow the exception and pass on each iteration.

Note the difference if you catch Exception only, which does not suppress a BaseException:

>>> from sys import exit
>>>
>>> def func(q):
... def funct(y):
... try:
... print(y)
... exit()
... except Exception:
... pass
... funct(q)
...
>>> a = ['1','2','3','4','5','6','7']
>>> for x in a: func(x)
...
1 # exits

Why does my exit() command not work in python?

exit() (some forms; it's not actually supposed to be used in scripts, so it can be overridden to do weird things; you want sys.exit()) is implemented by raising a SystemExit exception (exiting without bubbling out would mean cleanup blocks in finally and with statements don't execute, which would be bad, so they use the exception mechanism to do the cleanup). SystemExit is special in two ways:

  1. It doesn't dump a traceback and exit with a fixed status 1 if unhandled (it just silently exits the program with the status code it was provided)
  2. It's not a child of Exception, so except Exception:, which is very broad, still won't catch it

But you used a bare except, which catches everything, including SystemExit. Don't do that.

In this specific case, the correct solution is really to just replace exit() with break, which will pop you out of the while loop and allow the program to run to completion with no need for an exception at all. And use a narrower except clause while you're at it.

Exit codes in Python

You're looking for calls to sys.exit() in the script. The argument to that method is returned to the environment as the exit code.

It's fairly likely that the script is never calling the exit method, and that 0 is the default exit code.

Why python doesn't exit the script?

You should use sys.exit("optional custom message") instead of just exit()

This raises a SystemExit exception, while just exit() only makes sense in the context of the interpreter.

import sys
# logic here
if "something bad":
sys.exit("optional custom message")

The difference is described in detail here! https://stackoverflow.com/a/19747562/4541045



Related Topics



Leave a reply



Submit