Register Login

How to end a Python Program?

Updated Jun 13, 2020

While running a script in Python, you might be unaware of the fact that the code has an infinite loop. So, when you run the program it goes into an infinite loop. In such a situation you will have to halt the code execution.

In this article, we will look at the different ways to end a running script in Python.

Ways to end a program in Python

Using KeyboardInterrupt

If you want to exit the running program, then you need to raise a KeyboardInterrupt to terminate it.

For Windows, press CTRL + C.

For Linux systems, press CTRL + Z

If the KeyboardInterrupt does not work, then you can send a SIGBREAK signal. On Windows, press CTRL + Pause/Break. This might be handled by the interpreter and it might not generate a catchable KeyboardInterrupt exception.

In a Linux or Unix environment, you can find the process's PID (process identifier) for the program which you are running. Then kill the process by using its PID. You can use ps aux | grep python to determine the running Python processes and then use kill <pid> command for sending a SIGTERM signal to the system.

The commands are:

ps aux | grep python

kill -9 <pid>

To kill the program by file name:

pkill -f python-script-name.

But for Windows users, handling process signals is not so easy. If you are working on Windows, you have to terminate the executing process using the TerminateProcess function. The best way to do it is by opening the Task Manager. Locate the python.exe process that corresponds to your Python script, and click the "End Process". This will terminate the program forcibly.

The taskkill command can also be used for similar purposes. This command allows users on any version of Windows to terminate or kill any task using the process ID of the task. You have to type the code for this command in the command line. For Example,

c:\>taskkill /IM mspaint.exe

This will kill all processes running mspaint.exe.

Program exit in code

Using “os.exit()”

This method is used to terminate the process with the specified status. It is usually called for a child process after the os.fork() method is used. You can use this without flushing stido buffers or calling any cleanup handlers.

Example:

# Python program to show exit(0) program

import os

# Run for loop to print number 1 to 10
for i in range(10):

    # Exit the program if value of i equal to 5 
    if i == 5:
             
        # Prints the exit message 
        print(exit) 
        os._exit(0) 
    print(i)

Output:

0
1
2
3
4

Use exit() or Ctrl-Z plus return to exit

Using “sys.exit()”

The sys.exit() method allows you to exit from a Python program. The method takes an optional argument, which is an integer. This specifies an exit status of the code. An exit status of 0 is considered to be a successful termination.

Example:

# Python program to show sys.exit() program

import sys

# Run for loop to print number 1 to 10
for i in range(10):

    # Exit the program if value of i equal to 5 
    if i == 5:
             
        # Prints the exit message 
        print(exit) 
        sys.exit(0) 
    print(i)

Output:

0
1
2
3
4

Use exit() or Ctrl-Z plus Return to exit

Using “raise SystemExit”

SystemExit is an exception which is raised when the program you are running needs to stop. You use this exception to raise an error.  

# Python program to show raise SystemExit method

# Run for loop to print number 1 to 10
for i in range(10):

    # Exit the program if value of i equal to 5 
    if i == 5:
             
        # Prints the exit message 
        print(exit) 
        raise SystemExit 
    print(i)

Output:

0
1
2
3
4

Use exit() or Ctrl-Z plus Return to exit

Conclusion

Apart from the different ways discussed above to end a Python program, you can also use the built-in Python method quit(). For this method, you do not have to import any library and this can be simply included in any code.


×