Showing results for python exit script
Search instead for python3 exit script
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.

Answer from pjz on Stack Overflow
Top answer
1 of 14
1856
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".

🌐
Low Orbit
low-orbit.net › python-how-to-exit-end-a-program
Python How To Exit / End A Program | Low Orbit Flux
The function sys.exit() function is meant to be called from within scripts.
People also ask

How do I end a Python script?
To end python script, you can use the `sys.exit()` function so the program can finish properly and any required cleaning up takes place. If you want to use a user-initiated stop, simply press `Ctrl + C` while the code is running.
🌐
studyunicorn.com
studyunicorn.com › blogs › how-to-end-a-python-script-guide-for-beginners
How to End a Python Script in 2025 | Beginner's Guide
How can I end a Python script based on a condition?
You may use `sys.exit()` inside an `if` statement to end Python script based on a set condition. Add code such as `if error: sys.exit("Error occurred.")` to stop the script when a specific error happens.
🌐
studyunicorn.com
studyunicorn.com › blogs › how-to-end-a-python-script-guide-for-beginners
How to End a Python Script in 2025 | Beginner's Guide
What’s the difference between exit() and sys.exit()?
The `exit()` function is mostly meant for interactive sessions, helping end scripts quickly. In comparison, you can use `sys.exit()` in scripts that need to provide an exit code for more controlled program closure.
🌐
studyunicorn.com
studyunicorn.com › blogs › how-to-end-a-python-script-guide-for-beginners
How to End a Python Script in 2025 | Beginner's Guide
🌐
Medium
medium.com › @pies052022 › how-to-exit-a-python-program-easy-ways-for-beginners-8808340785af
How to Exit a Python Program: Easy Ways for Beginners | by JOKEN VILLANUEVA | Mar, 2026 | Medium
March 5, 2026 - When you press CTRL + C on the terminal while a script is running, the script terminates and raises an exception. To exit a Python program, we can use the built-in quit() method because this function completely ends the 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
It works in informal situations (like the Python shell) but isn’t appropriate for day-to-day operations. sys.exit(): Like an employee clocking out properly, saving work, locking their desk, and logging time. It follows protocol and ensures everything shuts down cleanly. Ideal for real-world scripts and automation.
🌐
Studyunicorn
studyunicorn.com › blogs › how-to-end-a-python-script-guide-for-beginners
How to End a Python Script in 2025 | Beginner's Guide
February 14, 2026 - It is meant for cases where you are sure you do not want the system to clean up after your application. If you want to python end script in action, press the keyboard interruption signal.
🌐
Tutor Python
tutorpython.com › end-a-program-in-python
Here is how to end a Program in Python - Tutor Python
October 21, 2024 - Allowing a script to run to completion is often the most straightforward way to end a program, especially for simple tasks or scripts that do not require complex error handling or conditional exits. In this article, we explored various way on how to end a Python program, including using sys.exit(), quit(), and exit(), handling Ctrl + C interruptions, raising exceptions, and naturally reaching the end of a script.
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.4 documentation
As well as the while statement just introduced, Python uses a few more that we will encounter in this chapter. if Statements: Perhaps the most well-known statement type is the if statement. For exa...
🌐
MyScale
myscale.com › blog › python-exit-program-functions-tips-revealed
Master Python Exit Program Functions: Tips Revealed
April 27, 2024 - 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.
🌐
Academic Help
academichelp.net › coding › python › how-to-exit-script.html
Exit Python Script: Termination Methods and Best Practices
September 4, 2023 - The exit() function in Python provides a straightforward way to exit or terminate a running script or program. It allows you to stop the execution of the program at any point by calling the function.
🌐
Altcademy
altcademy.com › blog › how-to-end-a-program-in-python
How to end a program in Python
September 11, 2023 - Again, "Goodbye, World!" won't be printed as the quit() function tells the Python interpreter to stop before it gets there. os._exit() is another way to exit a program in Python. It's a little bit different because it doesn't do any cleanup before exiting, it just ends things abruptly.
🌐
GeeksforGeeks
geeksforgeeks.org › python › why-does-python-automatically-exit-a-script-when-its-done
Why does Python automatically exit a script when it’s done? - GeeksforGeeks
July 23, 2025 - The atexit.register() method takes a function as an argument that is to be executed at script exit. Python3 · import atexit a = 5 print("Twice of a:", a*2) atexit.register(print, "Exiting Python Script") Output: Twice of a: 10 Exiting Python Script · Comment · Article Tags: Article Tags: Python ·
🌐
Kodeclik
kodeclik.com › how-to-end-a-python-program
End a Python Program in 3 ways
October 16, 2024 - There are three ways to end a Python program. 1. Do nothing and allow the program to reach the end. 2. Use the sys.exit() function. 3. Raise a SystemExit exception.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-exit-a-python-program-in-the-terminal
How to Exit a Python Program in the Terminal - GeeksforGeeks
July 23, 2025 - Exiting a Python program might seem like a straightforward task, but there are several ways to do it depending on the context and requirements of your script. Whether you want to gracefully shut down your application or terminate it immediately, knowing the right approach can help you manage ...
🌐
TestMu AI Community
community.testmuai.com › ask a question
How can I stop a Python script execution mid-way? - Ask a Question - TestMu AI Community
January 14, 2025 - Is it possible to stop the execution of a Python script at any point with a command? For example: some code quit() # stop execution at this point some more code (which will not be executed) How can I achieve this i…
🌐
McNeel Forum
discourse.mcneel.com › grasshopper developer
How to exit from a Python code? - Grasshopper Developer - McNeel Forum
May 5, 2019 - Hello, If your code is written in a GHPython node, is there a way to exit from the code in the middle of the code? You can exist from a function by “return”. You can exit from a for loop by “break” Buf when your code…
🌐
Domyassignments
domyassignments.com › home › effective techniques to end python programs
Effective Techniques to End Python Programs
June 28, 2024 - The sys.exit() method plays a pivotal role in Python for ensuring a graceful and controlled termination of the program. This method is a part of the sys module and is specifically designed to halt the execution of a Python script.
🌐
JanBask Training
janbasktraining.com › community › python-python › how-to-exit-a-python-script-in-an-if-statement1
How to exit a python script in an If statement? | JanBask Training Community
August 8, 2023 - A - 10 While True: If a>5: Print (“within if statement”) Break #exit if statement in Python Print (“ if statement not exited”) Else: print(“if statement still not exited”) Print (“if statement exited!”)
🌐
Replit
replit.com › home › discover › how to end a program in python
How to end a program in Python | Replit
February 6, 2026 - Even when the script is stopped with exit(), the registered handlers are still called, ensuring a clean shutdown. This approach is particularly useful when creating log files in Python that need to be properly closed and finalized.
🌐
Squash
squash.io › how-to-end-python-programs-correctly
How to End Python Programs
Related Article: How to Use Static Methods in Python · To exit a Python program, you can use the exit() function.