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

Python always displaying exit code 0
That message isn't coming from your program or from the Python interpreter. It's coming from whatever you're using to run your program, such as an IDE. Every process has an exit code when it terminates. By convention, a code of 0 means "success" or "normal termination". Even if there are "issues" with your code, it will exit with code 0 if it terminates normally (which means not throwing an uncaught exception or explicitly calling sys.exit). If your program is throwing an exception, but you're still seeing "exit code 0", then that indicates a bug or misconfiguration with however your program is being invoked. For instance, if your IDE is running a shell script which in turn runs the Python interpreter, then the shell script might exit normally even if the Python interpreter fails. More on reddit.com
🌐 r/learnprogramming
3
0
October 20, 2023
python - How do I terminate a script? - Stack Overflow
This will exit with status code 0; if you don't want that, you can pass a different one or a message: ... Exit from Python. More on stackoverflow.com
🌐 stackoverflow.com
Terminate code
Your program will terminate normally when you reach the end. You just have to make sure it reaches the end. Without seeing what you're doing, it's hard to give advice. More on reddit.com
🌐 r/learnpython
25
19
September 21, 2023
The Python process failed (exit code: 2)
Hi, I'm new to this tool and was following tutorials from Quick Start courses. When I was trying build the flow, I got the following error: More on community.dataiku.com
🌐 community.dataiku.com
November 7, 2023
🌐
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 ...
🌐
Notes
henryleach.com › 2025 › 02 › controlling-python-exit-codes-and-shell-scripts
Controlling Python Exit Codes and Shell Scripts | Notes
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
🌐
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 - The exit() in Python is defined as exit commands in python if in site.py and it works only if the site module is imported so it should be used in the interpreter only. It is like a synonym for quit() to make Python more user-friendly. It too gives a message when printed and terminate a program in Python. Example: In the provided code, when i is equal to 5, it prints "exit" and attempts to exit the Python interpreter using the exit() function.
🌐
Reddit
reddit.com › r/learnprogramming › python always displaying exit code 0
r/learnprogramming on Reddit: Python always displaying exit code 0
October 20, 2023 -

Hi, so i'm mostly new to coding and i have an initial test for a course this sunday. I'm using Python for it and what i wanna ask is, why does the "Process finished with exit code 0" message get displayed when there are certainly lots of issues with the code that even i as a noob can see? It often also comes without an output. I can place a single example, but it happens damn near every time.

Top answer
1 of 2
2
That message isn't coming from your program or from the Python interpreter. It's coming from whatever you're using to run your program, such as an IDE. Every process has an exit code when it terminates. By convention, a code of 0 means "success" or "normal termination". Even if there are "issues" with your code, it will exit with code 0 if it terminates normally (which means not throwing an uncaught exception or explicitly calling sys.exit). If your program is throwing an exception, but you're still seeing "exit code 0", then that indicates a bug or misconfiguration with however your program is being invoked. For instance, if your IDE is running a shell script which in turn runs the Python interpreter, then the shell script might exit normally even if the Python interpreter fails.
2 of 2
1
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
Find elsewhere
🌐
Quora
quora.com › What-is-the-exit-status-code-of-a-Python-script
What is the exit status code of a Python script? - Quora
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 the script author.
🌐
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 - Optionally, an exit status code can be passed as an argument, providing additional information about the reason for termination. By adhering to these best practices, you can effectively utilize the sys.exit() function in Python to stop a program when necessary.
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".

🌐
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 - Working with exit codes between Python & Shell Scripts What is an exit code? An exit code is received when a command or a script is executed. An exit code is a system response reporting success …
🌐
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.
🌐
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
While sys.exit() ensures proper ... terminate instantly, bypassing all cleanup routines and exception handling, os._exit() is the go-to command....
🌐
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.
🌐
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 ...
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.3 documentation
This exception is raised by the sys.exit() function. It inherits from BaseException instead of Exception so that it is not accidentally caught by code that catches Exception. This allows the exception to properly propagate up and cause the interpreter to exit.
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › python exit commands
Python Exit Commands | Interview Kickstart
November 7, 2024 - Python exit commands explained: sys.exit(), quit(), Ctrl+D, and best practices for graceful program termination in scripts and REPL.
🌐
Dataiku Community
community.dataiku.com › questions & discussions › using dataiku
The Python process failed (exit code: 2) — Dataiku Community
November 7, 2023 - [18:46:22] [INFO] [dku.flow.activity] - Run thread failed for activity recipe_from_notebook_admins_Python_notebook_on_job_postings_prepared_NP com.dataiku.dip.exceptions.ProcessDiedException: The Python process failed (exit code: 2). More info might be available in the logs.
🌐
MyScale
myscale.com › blog › python-exit-program-functions-tips-revealed
Master Python Exit Program Functions: Tips Revealed
When it comes to simple exits in Python, exit() and quit() serve as quick solutions to terminate a script. These functions are handy during interactive sessions or debugging processes. However, they are not recommended for production code due to their limited functionality.
🌐
pytest
docs.pytest.org › en › stable › reference › exit-codes.html
Exit codes - pytest documentation
Python version support · Sponsor ... Documentation · Back to top · Running pytest can result in six different exit codes: Exit code 0: All tests were collected and passed successfully ·...