It seems that python supports many different commands to stop script execution.
The choices I’ve found are:
Have I missed any?
What’s the difference between them? When would you use each?
Basically they all do the same thing, however, it also depends on what you are doing it for.
I don’t think you left anything out and I would recommend getting used to
You would use
os._exit() mainly if you are using big files or are using python to control terminal.
Otherwise mainly use
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
quit() are only available if the
site module is imported (docs).
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
quit() in the REPL.
sys.exit() in scripts, or
raise SystemExit() if you prefer.
os._exit() for child processes to exit after a call to
All of these can be called without arguments, or you can specify the exit status, e.g.,
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.
exit() are callable instance objects, but I think it’s okay to call them functions.
Let me give some information on them:
Furthermore, if you print it, it will give a message:
>>> print (quit) Use quit() or Ctrl-Z plus Return to exit >>>
This functionality was included to help people who do not know Python. After all, one of the most likely things a newbie will try to exit Python is typing in
quit should not be used in production code. This is because it only works if the
site module is loaded. Instead, this function should only be used in the interpreter.
exit() is an alias for
quit (or vice-versa). They exist together simply to make Python more user-friendly.
Furthermore, it too gives a message when printed:
>>> print (exit) Use exit() or Ctrl-Z plus Return to exit >>>
exit is considered bad to use in production code and should be reserved for use in the interpreter. This is because it too relies on the
sys.exit() also raises the
SystemExit exception. This means that it is the same as
exit in that respect.
Unlike those two however,
sys.exit is considered good to use in production code. This is because the
sys module will always be there.
os._exit() exits the program without calling cleanup handlers, flushing stdio buffers, etc. Thus, it is not a standard way to exit and should only be used in special cases. The most common of these is in the child process(es) created by
Note that, of the four methods given, only this one is unique in what it does.
Summed up, all four methods exit the program. However, the first two are considered bad to use in production code and the last is a non-standard, dirty way that is only used in special scenarios. So, if you want to exit a program normally, go with the third method:
Or, even better in my opinion, you can just do directly what
sys.exit does behind the scenes and run:
This way, you do not need to import
However, this choice is simply one on style and is purely up to you.
sys.exit is the canonical way to exit.
sys.exit just raises
SystemExit. However, calling
sys.exitis more idiomatic than raising
os.exit is a low-level system call that exits directly without calling any cleanup handlers.
exit exist only to provide an easy way out of the Python prompt. This is for new users or users who accidentally entered the Python prompt, and don’t want to know the right syntax. They are likely to try typing
quit. While this will not exit the interpreter, it at least issues a message that tells them a way out:
>>> exit Use exit() or Ctrl-D (i.e. EOF) to exit >>> exit() $
This is essentially just a hack that utilizes the fact that the interpreter prints the
__repr__ of any expression that you enter at the prompt.