How to stop a program in Python – with example

  • Reading time:15 mins read

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.

 

How to stop a program in Python

 

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.

 

How to stop a program in Python

 

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.

 

How to stop a program in Python

 

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

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.