It seems that python supports many different commands to stop script execution.
The choices I've found are: quit()
, exit()
, sys.exit()
, os._exit()
Have I missed any? What's the difference between them? When would you use each?
4 Answers 4
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 the site
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 the site
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 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 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.
-
3But in ipython shell,
quit
andexit
quit the shell whilesys.exit
doesn't.wsdzbm– wsdzbm2016年05月25日 17:41:56 +00:00Commented May 25, 2016 at 17:41 -
26
sys.exit()
is not a reliable way to close down. If it is called inside a thread, it will terminate only that thread unless it is in the main thread. This can lead to a lot of cases where the program continues because the call was not in the main thread, especially as some interpreters will invisibly thread calls.Elliot– Elliot2018年03月16日 16:47:05 +00:00Commented Mar 16, 2018 at 16:47 -
3I vote for adding an
end
command which wouldraise SystemExit
. Don't see why there can't be something simple like that, as in BASIC.Brian Burns– Brian Burns2018年10月09日 10:21:26 +00:00Commented Oct 9, 2018 at 10:21 -
17@BrianBurns: It's not worth adding dedicated syntax and reserving another keyword. This is similar to why
print
was changed from a statement to a function. It's easy to add syntax, but there's a complexity cost in doing so.user2357112– user23571122018年12月07日 19:58:50 +00:00Commented Dec 7, 2018 at 19:58 -
8
raise SystemExit
is fantastic even if the sys library is not available in some restricted python environmentradke– radke2019年03月13日 18:48:11 +00:00Commented Mar 13, 2019 at 18:48
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()
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.
-
I run my scripts from the Spyder console using
runfile
. Asys.exit()
in the script yields the same verbose messages as it would at the command line, i.e.,An exception occurred \\ SystemExit \\ User Warning: ....
user2153235– user21532352025年04月30日 19:58:25 +00:00Commented Apr 30 at 19:58
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()
.
-
13"Otherwise mainly use exit() or quit()." Even though the documentation states "They are useful for the interactive interpreter shell and should not be used in programs."?wovano– wovano2020年08月12日 11:35:16 +00:00Commented Aug 12, 2020 at 11:35
-
5There is also
exit("Here is why the program failed")
, which prints the error message and exits with return status1
.Jasha– Jasha2021年06月10日 21:48:29 +00:00Commented Jun 10, 2021 at 21:48 -
2"
quit()
: Closes the python file." ???Alex Peters– Alex Peters2022年04月17日 07:55:26 +00:00Commented Apr 17, 2022 at 7:55
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.
os.abort()