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
Discussions

bash - How to return status code in Python without actually exiting the process? - Stack Overflow
I'm trying to return a status code without exiting the process of my script. Is there an equivalent to sys.exit() that does not stop execution ? What I'd like to happen : If no exception is raised, More on stackoverflow.com
🌐 stackoverflow.com
Setting exit code in Python when an exception is raised - Stack Overflow
$ cat e.py raise Exception $ python e.py Traceback (most recent call last): File "e.py", line 1, in raise Exception Exception $ echo $? 1 I would like to change this exit code ... More on stackoverflow.com
🌐 stackoverflow.com
is raise SystemExit(exit_code) the right way to return a non-zero exit status?
I would like to control the exit status of a command. I saw that different exceptions are available from Click which are mostly used for bad command line usage. I just want a clean exit for a prope... More on github.com
🌐 github.com
12
December 12, 2014
What is Python's default exit code? - Stack Overflow
Martin Broadhurst's answer (tracing more through the code) confirms the Py_Main documentation. 2016-03-22T20:37:19.49Z+00:00 ... PyRun_AnyFileExFlags() in Python/pythonrun.c will exit() in the event of a SystemExit exception and so will not return if the script sets an exit code. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
bugs.python.org › issue27035
Issue 27035: Cannot set exit code in atexit callback - Python tracker
May 16, 2016 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/71222
🌐
GeeksforGeeks
geeksforgeeks.org › python-exit-commands-quit-exit-sys-exit-and-os-_exit
Python exit commands: quit(), exit(), sys.exit() and os._exit() - GeeksforGeeks
December 31, 2019 - os._exit() method in Python is used to exit the process with specified status without calling cleanup handlers, flushing stdio buffers, etc. Note: This method is normally used in child process after os.fork() system call.
🌐
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 ...
Find elsewhere
🌐
GitHub
github.com › pallets › click › issues › 270
is raise SystemExit(exit_code) the right way to return a non-zero exit status? · Issue #270 · pallets/click
December 12, 2014 - I just want a clean exit for a properly ran command (with no Abort! message). I ended up using raise SystemExit(1). Is this the proper way of doing it?
Author   aconrad
🌐
Notes
henryleach.com › 2025 › 02 › controlling-python-exit-codes-and-shell-scripts
Controlling Python Exit Codes and Shell Scripts - Henry Leach
February 9, 2025 - Also note that some programs, like grep(1) exit 0 only when something has been found, 1 when nothing has been found (but this doesn't mean an error) and >1 when there has been an error2. If you choose to try and make the error codes to your program mean something (and document this!) then you can use it in more complex scripts like: #!/bin/sh eval python exit-examples.py return_code=$? if [ $return_code = 0 ]; then echo "Success!" elif [ $return_code = 1 ]; then echo "Mild panic!" elif [ $return_code = 42 ]; then echo "Other fallback" else echo "Real Failure" exit $return_code fi
Top answer
1 of 4
11

sys.exit documents a default exit status of 0, and os._exit's docs specify a UNIX-like OS constant for "normal" exit status, os.EX_OK, but there is no documented guarantee I can find for the exit status in general.

Aside from that, the best I can give you is that in CPython, the python executable (including python.exe/pythonw.exe on Windows) is implemented in python.c by calling Py_Main and returning whatever it returns; per the documented guarantees on Py_Main, the exit status is:

0 if the interpreter exits normally (i.e., without an exception), 1 if the interpreter exits due to an exception, or 2 if the parameter list does not represent a valid Python command line.

Note that if an otherwise unhandled SystemExit is raised, this function will not return 1, but exit the process, as long as Py_InspectFlag is not set.

so this implies that simply running off the end of the __main__ module without an active exception should always return 0 for CPython, though alternate interpreters are not technically required to do the same.

This tracks with the implied exit status rules expected of most applications; while nothing explicitly says Python has to follow those rules, it would be extremely unusual for a tool that grew up in the command line UNIX-like world to violate those conventions.

2 of 4
9

If you look at the cpython source code:

  1. main() in Programs/python.c returns the return value of Py_Main()
  2. Py_Main() in Modules/main.c returns the return value of run_file()
  3. run_file(), also in Modules/main.c returns 0 unless PyRun_AnyFileExFlags() returns non-zero
  4. PyRun_AnyFileExFlags() in Python/pythonrun.c will exit() in the event of a SystemExit exception and so will not return if the script sets an exit code. It will only return non-zero if there is an internal error.

So the return value of run_file() is what makes the default exit code of a script 0.

🌐
Quora
quora.com › What-is-the-exit-status-code-of-a-Python-script
What is the exit status code of a Python script? - Quora
The sys.exit() function raises a SystemExit exception to exit the program, so try statements and cleanup code can execute. The os._exit() version doesn't do this. It just ends the program without doing any cleanup or flushing output buffers, so it shouldn't normally be used. The Python docs indicate that os._exit() is the normal way to end a child process created with a call to os.fork(), so it does have a use in certain circumstances.
🌐
Linux Hint
linuxhint.com › python-exit-codes
Python Exit Codes – Linux Hint
We can see that the program does return the desired output. On Unix systems, we can use the echo command followed by the environment variable ?. This environment variable allows you to get the exit code of the last executed command. In our case, the last command is the Python program:
🌐
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
Key characteristics of the os._exit() command are: ... In this example, the last print() statement is never reached. The program exits abruptly with status code 1, and no cleanup code is executed.
🌐
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 ...
🌐
Quora
quora.com › Is-there-any-way-to-close-a-python-script-without-using-the-exit-command
Is there any way to close a python script without using the 'exit()' command? - Quora
Yes. Python programs can be terminated without calling exit(). Options vary by context (interactive shell, script, thread, GUI, child process). Common methods: ... Let the script reach the end of the main block or the top-level code finish.
🌐
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 - So it’s best to learn this shortcut, rather than use the Python-specific exit(). Then you can exit bash, zsh, ipython, sqlite, and any other command line program, without giving it a second thought. The os._exit(n) function exits Python immediately with the given exit code n.
🌐
DataCamp
datacamp.com › tutorial › how-to-exit-python-a-quick-tutorial
How to Exit Python: A Quick Tutorial | DataCamp
April 5, 2024 - In production code, it is common practice to use the sys.exit() function from the sys module to exit Python without relying on the site module. The sys module is generally available but you will need to import it.
🌐
Python
docs.python.org › 3 › library › atexit.html
atexit — Exit handlers
The atexit module defines functions to register and unregister cleanup functions. Functions thus registered are automatically executed upon normal interpreter termination. atexit runs these functio...