How to Exit a Python Program: sys.exit()
To exit a Python program before it completes, use sys.exit()
.
This article also covers the built-in functions exit()
and quit()
for ending a Python REPL (interactive mode) session, and shows how to forcefully stop a running program from the terminal or command prompt using the Ctrl + C
shortcut—especially useful when a program becomes stuck in an infinite loop.
Exit a Python program: sys.exit()
Basic usage
When sys.exit()
is called, the Python program terminates immediately.
Any lines of code following sys.exit()
will not be executed.
import sys
print('Start')
sys.exit()
print('Finish')
$ python sys_exit.py
Start
You can combine it with an if
statement to make the program exit only under specific conditions.
import sys
a = 100
print('Start')
if a == 0:
sys.exit()
print('Continue')
if a == 100:
sys.exit()
print('Finish')
$ python sys_exit_if.py
Start
Continue
Specify exit status or error messages
You can pass an argument to sys.exit()
to specify the program’s exit status.
If no argument is passed, the exit status is 0
, indicating a normal termination. To indicate an abnormal termination, pass a non-zero integer.
If a non-integer is passed, it is printed to standard error (stderr), and the exit status is set to 1
. For example, passing a string allows you to display an error message.
import sys
sys.exit('Error message')
$ python sys_exit_stderr.py
Error message
You can confirm that the message is output to stderr by redirecting stdout and stderr to separate files like this:
$ python sys_exit_stderr.py 1> data/temp/stdout.txt 2> data/temp/stderr.txt
In this example, stdout.txt
will be empty, while stderr.txt
will contain Error message
.
$ cat data/temp/stdout.txt
$ cat data/temp/stderr.txt
Error message
In the example, the cat
command displays the contents of a file on UNIX systems. On Windows, you would use type
instead.
The SystemExit
exception
sys.exit()
raises a SystemExit
exception.
If sys.exit()
is called inside a try
block, a bare except clause (without specifying the exception type) will catch the SystemExit
exception, preventing the program from exiting.
import sys
print('Start')
try:
sys.exit()
except:
print('Catch all exceptions')
print('Finish')
$ python sys_exit_systemexit.py
Start
Catch all exceptions
Finish
To handle exceptions properly, always specify the expected exception type or use the base class Exception
. For more on exception handling, see the article below:
Exit the REPL (interactive mode): exit()
, quit()
To exit the Python REPL (interactive mode), you can use the built-in functions exit()
and quit()
.
- Built-in Constants - exit() — Python 3.13.3 documentation
- Built-in Constants - quit() — Python 3.13.3 documentation
$ python
Python 3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
Note that exit()
and quit()
are intended for use in the REPL only and should not be used in Python scripts (.py
files).
Also, if you type exit
or quit
without parentheses, you'll see a message suggesting you can also use Ctrl + D
to exit.
$ python
Python 3.12.4 (main, Jun 6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> ^D
Forcefully terminate a running Python program: Ctrl + C
To forcefully stop a running Python program in the terminal or command prompt, use the keyboard shortcut Ctrl + C
.
This is especially useful when your program gets stuck, for example, in an infinite loop.
Simply press Ctrl + C
while the program is running, and it will terminate immediately.