Python exit commands - why so many and when should each be used?
Solution 1:
Let me give some information on them:
-
quit()
simply raises theSystemExit
exception.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
.Nevertheless,
quit
should not be used in production code. This is because it only works if thesite
module is loaded. Instead, this function should only be used in the interpreter. -
exit()
is an alias forquit
(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 >>>
However, like
quit
,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 thesite
module. -
sys.exit()
also raises theSystemExit
exception. This means that it is the same asquit
andexit
in that respect.Unlike those two however,
sys.exit
is considered good to use in production code. This is because thesys
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 byos.fork
.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: sys.exit
.
Or, even better in my opinion, you can just do directly what sys.exit
does behind the scenes and run:
raise SystemExit
This way, you do not need to import sys
first.
However, this choice is simply one on style and is purely up to you.
Solution 2:
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.
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()
orquit()
in the REPL.Use
sys.exit()
in scripts, orraise SystemExit()
if you prefer.Use
os._exit()
for child processes to exit after a call toos.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.
Solution 3:
Different Means of Exiting
os._exit()
:
- Exit the process without calling the cleanup handlers.
exit(0)
:
- a clean exit without any errors / problems.
exit(1)
:
- There was some issue / error / problem and that is why the program is exiting.
sys.exit()
:
- When the system and python shuts down; it means less memory is being used after the program is run.
quit()
:
- Closes the python file.
Summary
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 quit()
or exit()
.
You would use sys.exit()
and os._exit()
mainly if you are using big files or are using python to control terminal.
Otherwise mainly use exit()
or quit()
.
Solution 4:
sys.exit
is the canonical way to exit.
Internally sys.exit
just raises SystemExit
. However, calling sys.exit
is more idiomatic than raising SystemExit
directly.
os.exit
is a low-level system call that exits directly without calling any cleanup handlers.
quit
and 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 exit
or 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.