How to Use the Python sys.exit() Method: With Examples

In this tutorial, we will focus on the exit function from the os module. This function provides an effective way of terminating a script or program. You can this of it pressing ALT + 4 or CTRL + Q on your program.

How to Use the Python sys.exit() Method: With Examples

Python is a powerful and intuitive language with a broad array of built-in libraries and modules that allow you to perform a wide range of tasks.

One of the most powerful and useful module in the Python standard library is the sys module. This module provides us access methods and variables to interact with the interpreter.

How it Works?

The function works by raising the SystemExit exception, which can be caught in the outer layers of the script to perform cleanup actions before the script actually terminates. If SystemExit is not caught, the Python interpreter will exit.

The primary use of sys.exit() is to quit from Python in case of an error or if a specific condition is met in your code.

Method Usage

We can use sys.exit() in two ways:

  1. sys.exit()- This will exit from Python without any exit status.
  2. sys.exit(n) - This will exit from Python with an exit status of n. Typically, an exit status of 0 indicates success, and any other value (usually 1) indicates an error.

Let us explore how to work with these two values with some examples.

Example 1 - Basic Usage.

The following example shows how to call the exit function to terminate a python program.

import sys

print("I am running!!")

print("I won't run!!")

In the code above, we import the sys module and print a simple string.

We then call the sys.exit() function to terminate the program execution. If you run the code above, you will notice the last print statement is not executed.

Using sys.exit() with status code.

We can also provide an input to the exit function which is used as status code. Providing a custom error code can help you to specify what type of error occurs and why. This is incredibly useful in debugging.

An example code demonstration is as shown below:

import sys

except SystemExit as e:
    print("Caught SystemExit with code:", e.code)

In the code above, sys.exit(1) attempts to exit the script with status code 1.

However, the call to sys.exit() is inside a try block, so the SystemExit exception it raises is caught by the except block, and the script continues to execute.

NOTE: Ensure that the provided parameter for the exit function is an integer value.

Use sys.exit() in conditional statements

As you can guess, we can also use the exit function inside conditional blocks for more granular and filtered error handling.

An example is as shown below:

import sys

url = input("provide the target url: ")

if url != '':

print("You're in!")

In this case, we prompt the user for the target url. If the provided input does not match the value, we exit and return an error code of 1.


We hope you enjoyed this tutorial on the sys.exit() function in the Python ecosystem. The exit() function is an effective way of terminating the program execution in a more graceful and deterministic manner.

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.