How to stop a program in Python
Exiting a program is the process of terminating an active python program from executing further statements.
Normally program execution implicitly comes to an end when all the code in that particular script has been executed to completion.
Using the sys module
However, a program may also exit when an error or exception is raised in the program. We can also make a program exit or terminate at a certain point using a set of functions provided in Python.
One of the most appropriate functions that we can use to exit from a Python program is the sys.exit() function.
This function is available in the sys module and when called it raises the SystemException in Python that then triggers the interpreter to stop further execution of the current python script.
To get started with this function we first need to import the sys module which will then allow us to access the exit() function.
import sys
Alternative import for sys module
Alternatively, we can also directly import the function using the keywords from and import along with the module name as shown below.
from sys import exit
Now, to explore how we can use this function we will set up a simple example using an if-else statement.
This if-else statement checks if a certain variable that we have named key is equal to a certain value and then prints a message ‘Hello’. As shown here.
If the variable is not equal to the value the program will return the message ‘Wrong key’.
Stop a program in Python — Example 2
Now outside the scope of the if-else statement, we also have a print statement that prints the message ‘this code runs down here’.
In the examples above it is clear that the print statement outside the if-else was executed in both scenarios i.e regardless of whether the variable was matched or not matched.
Stop a program in Python by variable status
Suppose we wanted to stop the program from executing any further statements when the variable is not matched.
In such a scenario we can use the sys.exit() function to do so, here is how we can implement that.
In the example above, since the variable named key is not equal to 1234, the else block is executed and thus the program exits and no further statements are executed.
We can also exit the program and print a message to the standard output as shown below.
Stop a program in Python — Example 3
Please note that if the variable name key matches the value 1234 then in that case the if block will be executed and not the else block.
Since the if block does not have an exit function, it means that the message ‘Hello’ will be printed and also the print statement that follows the if-else statement will also be executed.
Summary
The sys.exit() is the standard way of exiting programs in python and is reliable even in production code.
This is how you can stop a program in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
