You're looking for calls to sys.exit(...) (exit(...) calls sys.exit(...)) in the script. The argument to that method is returned to the environment as the exit code.

It's fairly likely that the script is never calling the exit(...) method, and that 0 is the default exit code.

Answer from Dave Costa on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exit-commands-quit-exit-sys-exit-and-os-_exit
Python exit commands: quit(), exit(), sys.exit() and os._exit() - GeeksforGeeks
July 12, 2025 - If you print it, it will give a message and end a program in Python. Example: In the provided code, when i is equal to 5, it prints "quit" and attempts to exit the Python interpreter using the quit() function.
🌐
Codecademy
codecademy.com › article › python-exit-commands-quit-exit-sys-exit-os-exit-and-keyboard-shortcuts
Python Exit Commands: quit(), exit(), sys.exit(), os._exit() and Keyboard Shortcuts | Codecademy
Intended only for use in the Python interpreter or shell sessions. It is a helper function provided by the site module. If used in a standalone script, it can raise a NameError since the site module may not be loaded. ... This code prints even numbers starting from 2 and exits the program using quit() when it reaches the fifth even number, which is 10.
🌐
Linux Hint
linuxhint.com › python-exit-codes
Python Exit Codes – Linux Hint
In the example program above, we start by importing the exit function from the sys module. We then use the print statement to print some text on the screen. For the important part, we use the exit function and pass the exit code as 125. NOTE: As soon as Python encounters the exit() function, it will immediately terminate the program and return the exit code or message specified.
🌐
Delft Stack
delftstack.com › home › howto › python › exit codes in python
How to Exit Codes in Python | Delft Stack
March 11, 2025 - To utilize exit codes in Python, you typically use the sys.exit() function from the sys module. This function allows you to specify the exit code you want your script to return. Here’s a simple example to illustrate this concept:
🌐
Super Fast Python
superfastpython.com › home › tutorials › process exit codes in python
Process Exit Codes in Python - Super Fast Python
September 11, 2022 - You can set an exit code for a process via sys.exit() and retrieve the exit code via the exitcode attribute on the multiprocessing.Process class. In this tutorial you will discover how to get and set exit codes for processes in Python. Let’s get started. Need Process Exit Codes A process is a running instance of […]
🌐
Notes
henryleach.com › 2025 › 02 › controlling-python-exit-codes-and-shell-scripts
Controlling Python Exit Codes and Shell Scripts - Henry Leach
February 9, 2025 - $ python exit-examples.py; echo Returned $? Hello World Returned 1 · You could of course return any value you like between 0 and 255, but other than 0 being success, there's not much agreement what the various exit codes mean.
🌐
Python Morsels
pythonmorsels.com › exiting-a-python-program
Exiting a Python program - Python Morsels
February 21, 2022 - If you want to exit a Python program early, call the sys.exit function or raise a SystemExit exception. If you're trying to indicate an error code while exiting, pass a number from 0 to 127 to sys.exit, where 0 indicates success and anything else indicates an error.
Find elsewhere
🌐
Medium
medium.com › @anupkumarray › working-with-exit-codes-between-python-shell-scripts-177931204291
Working with exit codes between Python & Shell Scripts | by Anup Kumar Ray | Medium
October 17, 2021 - Since exception is properly handled in python code, it will exit with a success return code 0 when Exceptions.sh is executed.
🌐
Quora
quora.com › What-is-the-exit-status-code-of-a-Python-script
What is the exit status code of a Python script? - Quora
Answer (1 of 2): In Python, you can check the exit code of a script using the `sys.exit()` function provided by the `sys` module. By default, if your script runs without any issues, it will have an exit code of 0 (zero), which indicates success . The exit codes only have meaning as assigned by th...
🌐
freeCodeCamp
freecodecamp.org › news › python-exit-how-to-use-an-exit-function-in-python-to-stop-a-program
Python Exit – How to Use an Exit Function in Python to Stop a Program
June 5, 2023 - In this example, the program will print "Before exit", but when the exit() function is called with a status of 1, the program will terminate immediately without executing the remaining code. Therefore, the line "After exit" will not be printed. Let's now write a Python script and demonstrate ...
🌐
AskPython
askpython.com › home › exit a python program in 3 easy ways!
Exit a Python Program in 3 Easy Ways! - AskPython
April 7, 2023 - Another useful function to exit ... into Python programs. An additional feature of this function is that it can take a string as an argument and print it after it is executed. The sys.exit() function can be used at any point in time without worrying about corruption in the code. ... Let us have a look at the below example to understand ...
🌐
CodeRivers
coderivers.org › blog › python-exit-code
Python Exit Code: Understanding, Using, and Best Practices - CodeRivers
March 5, 2025 - In this example, if a ZeroDivisionError occurs, the program prints an error message and exits with an exit code of 1. If no errors occur, it reaches the end and exits with an exit code of 0. When running a Python script directly from the command line, the return value of the main() function ...
🌐
HashBangCode
hashbangcode.com › article › stopping-code-execution-python
Stopping Code Execution In Python | #! code
Here is another example that stops execution if the length of an array is less than 2. import sys listofitems = [] # Code here that does something with listofitems if len(listofitems) < 2: sys.exit('listofitems not long enough') As a side note, if you are looking to simply kill a python program that has taken over your terminal then press Ctrl+C to stop execution.
🌐
Adam Johnson
adamj.eu › tech › 2021 › 10 › 10 › the-many-ways-to-exit-in-python
The Many Ways to Exit in Python - Adam Johnson
October 10, 2021 - If you’re looking for a quick answer, you can stop reading here. Use raise SystemExit(<code>) as the obviousest way to exit from Python code and carry on with your life.
🌐
O'Reilly
oreilly.com › library › view › programming-python-second › 0596000855 › ch03s04.html
Program Exits - Programming Python, Second Edition [Book]
March 1, 2001 - In fact, explicitly raising the built-in SystemExit exception with a Python raise statement is equivalent to calling sys.exit. More realistically, a try block would catch the exit exception raised elsewhere in a program; the script in Example 3-11 exits from within a processing function.
Author   Mark Lutz
Published   2001
Pages   1296
Top answer
1 of 14
1855
import sys
sys.exit()

This will exit with status code 0; if you don't want that, you can pass a different one or a message:

sys.exit(1)

details from the sys module documentation:

sys.exit([arg])

Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

Note that this is the 'nice' way to exit. @glyphtwistedmatrix below points out that if you want a 'hard exit', you can use os._exit(*errorcode*), though it's likely os-specific to some extent (it might not take an errorcode under windows, for example), and it definitely is less friendly since it doesn't let the interpreter do any cleanup before the process dies. On the other hand, it does kill the entire process, including all running threads, while sys.exit() (as it says in the docs) only exits if called from the main thread, with no other threads running.

2 of 14
529

A simple way to terminate a Python script early is to use the built-in quit() function. There is no need to import any library, and it is efficient and simple.

Example:

#do stuff
if this == that:
  quit()

However, this relies on an implicit import of the site module. Per the docs:

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. They are useful for the interactive interpreter shell and should not be used in programs.

As such, if the -S flag is passed, this may raise a NameError with the message "name 'quit' is not defined".