Difference between exit() and sys.exit() in Python

Difference between exit() and sys.exit() in Python

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.
        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()).

If I use exit() in a code and run it in the shell, it shows a message asking whether I want to kill the program or not. Its really disturbing.
See here

But sys.exit() is better in this case. It closes the program and doesnt create any dialogue box.

Difference between exit() and sys.exit() in Python

Solution, Origins, Differences & Speed

Why do we need the exit() /
sys.exit() commands?

Usually, the code runs through the lines until the end and the program exists automatically.
Occasionally, we would like to ask the program to close before the full cycle run.
An example case is when you implement authentication and a user fails to authenticate, in some cases you would like to exit the program.

The exit()

Exits Python.

Maybe you didnt know this, but its a synonym of quit() and was added after quit() to make python more user friendly.
Designed to work with interactive shells.

Use the built-in exit() out of the box, as is, without importing any library.
Just type this:


Execution Time: 0.03s


  • Faster to use (built-in)
  • Works both with python 2.x and python 3.x
  • Fast
  • Can be used exactly like sys.exit() (with the exception)


  • No exception message

The sys.exit()

Exits Python and raising the SystemExit exception (requires an import).
Designed to work inside programs.


import sys

Execution Time (of just the import and sys.exit()): 0.07s

Or you can use a message for the SystemExit exception:

import sys
  sys.exit(This is an exit!)
except SystemExit as error:


This is an exit!


  • Triggers SystemExit exception
  • You can use an exception message
  • Closes without a dialog
  • Utilizes finally clause of try
  • Works both with python 2.x and python 3.x


  • Needs an import
  • Slower by 57.1% than exit()


If you dont need an exception with an optional message, then use exit(), this is faster and built-in.
If you require more functionality of an exception with an optional message, use sys.exit().

In the code examples I am using Python 3.x

Leave a Reply

Your email address will not be published. Required fields are marked *