exit
is a helper for the interactive shell - sys.exit
is intended for use in programs.
The site
module (which is imported automatically during startup, except if the -S
command-line option is given) adds several constants to the built-in namespace (e.g. exit
). They are useful for the interactive interpreter shell and should not be used in programs.
Technically, they do mostly the same: raising SystemExit
. sys.exit
does so in sysmodule.c:
static PyObject *
sys_exit(PyObject *self, PyObject *args)
{
PyObject *exit_code = 0;
if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
return NULL;
/* Raise SystemExit so callers may catch it or clean up. */
PyErr_SetObject(PyExc_SystemExit, exit_code);
return NULL;
}
While exit
is defined in site.py and _sitebuiltins.py, respectively.
class Quitter(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return 'Use %s() or %s to exit' % (self.name, eof)
def __call__(self, code=None):
# Shells like IDLE catch the SystemExit, but listen when their
# stdin wrapper is closed.
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)
__builtin__.quit = Quitter('quit')
__builtin__.exit = Quitter('exit')
Note that there is a third exit option, namely os._exit, which exits without calling cleanup handlers, flushing stdio buffers, etc. (and which should normally only be used in the child process after a fork()
).
The shebang line in any script determines the script's ability to be executed like a standalone executable without typing python
beforehand in the terminal or when double clicking it in a file manager (when configured properly). It isn't necessary but generally put there so when someone sees the file opened in an editor, they immediately know what they're looking at. However, which shebang line you use IS important.
Correct usage for (defaults to version 3.latest)Python 3 scripts is:
#!/usr/bin/env python3
Correct usage for (defaults to version 2.latest)Python 2 scripts is:
#!/usr/bin/env python2
The following should NOT be used (except for the rare case that you are writing code which is compatible with both Python 2.x and 3.x):
#!/usr/bin/env python
The reason for these recommendations, given in PEP 394, is that python
can refer either to python2
or python3
on different systems. It currently refers to python2
on most distributions, but that is likely to change at some point.
Also, DO NOT Use:
#!/usr/local/bin/python
"python may be installed at /usr/bin/python or /bin/python in those
cases, the above #! will fail."
--"#!/usr/bin/env python" vs "#!/usr/local/bin/python"
Best Answer
Let me give some information on them:
quit()
simply raises theSystemExit
exception.Furthermore, if you print it, it will give a message:
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:
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:This way, you do not need to import
sys
first.However, this choice is simply one on style and is purely up to you.