How to Exit a Program in Python
Sometimes you want a Python program to stop on purpose.
This page shows the main ways to do that, when to use each one, and what mistakes to avoid. You will also see the difference between stopping a loop, leaving a function, and ending the whole script.
Quick answer
import sys
print("Starting program")
sys.exit()
print("This line will not run")
Use sys.exit() when you want to stop the program early. It raises SystemExit and ends the script unless it is caught.
What this page helps you do
- Stop a Python script on purpose
- Exit early when a condition is met
- Understand the difference between stopping a loop and stopping the whole program
- Choose between
sys.exit(),raise SystemExit, andreturn
Use sys.exit() to end the program
The most common way to end a Python script is sys.exit().
You need to import the sys module first:
import sys
print("Program started")
user_age = -1
if user_age < 0:
print("Age cannot be negative")
sys.exit()
print("This line will not run")
What this does
import sysgives you access tosys.exit()- When Python reaches
sys.exit(), it stops the script - Any code after that line will not run
This is a common choice for command-line scripts.
If you want to learn more about the module itself, see the sys module overview. For a deeper explanation of this function, see sys.exit() explained.
Pass a message to sys.exit()
You can also pass a message:
import sys
name = ""
if name == "":
sys.exit("Name is required")
print("Hello", name)
In many terminal environments, the message is shown before the program ends.
Exit with a status code
Sometimes you want your program to tell the terminal or another program whether it succeeded.
That is what an exit status code is for.
import sys
password = "abc"
if len(password) < 8:
print("Password is too short")
sys.exit(1)
print("Password accepted")
sys.exit(0)
Common status codes
sys.exit(0)usually means successsys.exit(1)usually means an error- Any non-zero number usually means something went wrong
This matters most when your script is run:
- from the terminal
- by a shell script
- by another program
- by an automated tool
Check the exit code in a terminal
Run your script:
python your_script.py
Then check the exit status:
python your_script.py; echo $?
You can also test it directly:
python -c "import sys; sys.exit(0)"
python -c "import sys; sys.exit(1)"
These commands help you see how success and error codes work.
Use return instead of exiting the whole program
return does not stop the whole script. It only leaves the current function.
This is often the better choice when you are inside a helper function.
def greet(name):
if name == "":
print("No name provided")
return
print("Hello,", name)
greet("")
print("Program continues")
Output
No name provided
Program continues
This is cleaner than calling sys.exit() from deep inside a reusable function.
Use return when:
- you only want to stop the current function
- the rest of the program should continue
- you are writing reusable code
If you are not comfortable with functions yet, see Python functions explained.
break is not the same as exiting
A very common mistake is using break when you really want to stop the whole script.
break only stops the current loop.
for number in range(5):
if number == 2:
break
print(number)
print("Program continues after the loop")
Output
0
1
Program continues after the loop
If you want to stop repeating, use break.
If you want to stop the entire script, use sys.exit().
Here is the difference:
import sys
for number in range(5):
if number == 2:
sys.exit()
print(number)
print("This line will not run")
To learn more about loop control, see Python break and continue statements.
raise SystemExit as another option
sys.exit() works by raising a special exception called SystemExit.
So these are closely related:
import sys
sys.exit()
raise SystemExit
You can also include a message or code:
raise SystemExit("Stopping program")
For most beginners, sys.exit() is the clearer choice.
Still, it helps to know about SystemExit because you may see it in error output or in advanced code. See SystemExit exception in Python explained.
What happens in interactive environments
sys.exit() can look a little different depending on where you run your code.
Examples:
- IDLE
- Jupyter notebooks
- some IDEs
- the terminal
In a terminal script, the Python process usually ends completely.
In an IDE or notebook, the script may stop running, but the environment itself stays open.
That is normal. The important thing is this:
- code before
sys.exit()runs - code after
sys.exit()does not run
Common mistakes
Here are some common problems beginners run into.
Using break when you meant to stop the whole program
break only exits the current loop.
If you want to end the script, use sys.exit() instead.
Using return outside a function
This is not allowed in normal Python code.
return can only be used inside a function.
Correct:
def check_value(x):
if x < 0:
return
print(x)
Incorrect:
x = -1
if x < 0:
return
Forgetting to import sys
This will fail:
sys.exit()
This works:
import sys
sys.exit()
Expecting code after sys.exit() to still run
It will not.
import sys
print("Before exit")
sys.exit()
print("After exit")
Only "Before exit" is printed.
Catching SystemExit by accident
Because sys.exit() raises SystemExit, a broad exception handler can sometimes interfere with it.
import sys
try:
sys.exit()
except BaseException:
print("Caught something")
In this example, the program may not exit as expected because BaseException is very broad.
In beginner code, it is usually better to catch specific exceptions with try and except. See how to use try/except blocks in Python.
FAQ
What is the easiest way to exit a Python program?
Use sys.exit() in most scripts. It is clear and commonly used.
Does break exit the program?
No. break only stops the current loop.
Does return exit the program?
No. return exits the current function. The rest of the program may continue.
What does sys.exit(1) mean?
It ends the program with a non-zero exit status, which usually signals an error.
Why does my IDE stay open after sys.exit()?
The script stops, but the IDE or notebook environment itself usually keeps running.