. Advertisement .
..3..
. Advertisement .
..4..
You may need to exit a program prematurely when it has run for too long, or the rest of the code is no longer necessary. But you must do it safely and don’t leave your system in an unclean state.
Here are all the ways to quit Python scripts and the interpreter using built-in functions.
How To Quit Python Interpreter And Scripts
Ctrl+D
While ctrl+C stops whatever the Python interpreter is executing, pressing ctrl+D ends everything and quits the interpreter completely. This common shortcut offers the most convenient way to exit a program in most terminal applications, including the Python interpreter.
Both of those shortcuts raise the KeyboardInterrupt exception, which Python checks for regularly. Remember to press it only once, or you also exit the current shell instance of your command line.
quit() And exit()
In the default Python interpreter, you have two other options to end it: quit() and exit() functions.
They are provided by the site module, which comes with Python installations. It gets imported during the interpreter’s initialization and adds some constants to the built-in namespace, including quit and exit.
While both functions need no arguments, you can use them to print a message too.
>>> quit("There is an error.") # or exit("There is an error.")
There is an error.
They work in a similar way to the combination of a print and exit or quit function.
>>> print("There is an error.")
>>> quit() # or exit()
There is an error.
Keep in mind that the Python interpreter doesn’t import the site module when given the -S option. You will see an error when trying to execute quit() or exit(). For this reason, you should only use those two functions in the interactive interpreter, not in Python programs.
python -S
>>> quit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'quit' is not defined
>>> exit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'exit' is not defined
In that case, you will need to explicitly load the module and call its main() function first.
python -S
>>> import site
>>> site.main()
>>> quit() # or exit()
sys.exit() And os._exit()
These functions come from built-in sys and os modules and signal an intention to quit the Python program. They accept an argument – the exit status. It is optional for sys.exit() and required for os._exit().
>>> import sys
>>> sys.exit()
>>> import os
>>> os._exit(0)
The main difference between these exit functions is that sys.exit() raises a SystemExit exception while os._exit() directly relies on your system’s underlying exit function.
You should not use os._exit() as your default method to end a Python program unless it is a child process created by fork().
SystemExit Exception
This built-in exception is what the above functions rely on. The syntax is quite straightforward:
>>> raise SystemExit()
When catching this exception, your Python interpreter will trigger the exit routine. If SystemExit isn’t handled, the interpreter will stop with no traceback printed.
The sys.exit() function accepts the same arguments passed to the constructor of SystemExit. For instance, you can specify the exit status by giving it an integer value. The default status is zero (which presents success and is also passed to the exit() function of C).
On the other hand, values of another type will end up with the one exit status (failure). If the argument is a string, it will get printed before exiting.
>>> raise SystemExit("Error")
Error
>>> Print("Error")
>>> raise SystemExit(1)
Conclusion
You can rely on different functions to quit Python scripts or the interpreter. Most of them make use of the SystemExit exception to send your intention to quit the current program. Make sure you understand the underlying mechanism and select the proper method for every scenario.
[…] addition to quitting a program, the os module also provides common manipulation capabilities for pathnames. For example, […]