Как остановить скрипт python
In this article, we are going to see that how to exit a Python Script.
Exiting a Python script refers to the process of termination of an active python process. In this article, we will take a look at exiting a python program, performing a task before exiting the program, and exiting the program while displaying a custom (error) message.
Exiting a Python application
There exist several ways of exiting a python application. The following article has explained some of them in great detail.
Example: Exit Using Python exit() Method
Python3
Output:
Detecting Script exit
Sometimes it is required to perform certain tasks before the python script is terminated. For that, it is required to detect when the script is about to exit. atexit is a module that is used for performing this very task. The module is used for defining functions to register and unregister cleanup functions. Cleanup functions are called after the code has been executed. The default cleanup functions are used for cleaning residue created by the code execution, but we would be using it to execute our custom code.
6 ways to exit program in Python

Python is one of the most versatile and dynamic programming languages used out there. Nowadays, It is the most used programming language, and for good reason. Python gives a programmer the option and the allowance to exit a python program whenever he/she wants.
Table of Contents
Using the quit() function
A simple and effective way to exit a program in Python is to use the in-built quit() function. No external libraries need to be imported to use the quit() function.
This function has a very simple syntax:
When the system comes up against the quit() function, it goes on and concludes the execution of the given program completely.
The quit() function can be used in a python program in the following way:
The Python interpreter encounters the quit() function after the for loop iterates once, and the program is then terminated after the first iteration.
Using the sys.exit() function
The sys module can be imported to the Python code and it provides various variables and functions that can be utilized to manipulate various pieces of the Python runtime environment.
The sys.exit() function is an in-built function contained inside the sys module and it is used to achieve the simple task of exiting the program.
It can be used at any point in time to come out of the execution process without having the need to worry about the effects it may have on a particular code.
The sys.exit() function can be used in a python program in the following way:
Using the exit() function
There exists an exit() function in python which is another alternative and it enables us to end program in Python.
It is preferable to use this in the interpreter only and is an alternative to the quit() function to make the code a little more user-friendly.
The exit() function can be used in a python program in the following way:
The two functions, exit() and quit() can only be implemented if and when the site module is imported to the python code. Therefore, these two functions cannot be used in the production and operational codes.
The sys.exit() method is the most popular and the most preferred method to terminate a program in Python.
Using the KeyboardInterrupt command
If Python program is running in cosole, then pressing CTRL + C on windows and CTRL + Z on unix will raise KeyboardInterrupt exception in the main thread.
If Python program does not catch the exception, then it will cause python program to exit. If you have except: for catching this exception, then it may prevent Python program to exit.
If KeyboardInterrupt does not work for you, then you can use SIGBREAK signal by pressing CTRL + PAUSE/BREAK on windows.
In Linux/Unix, you can find PID of Python process by following command:
and you can kill -9 to kill the python process. kill -9 <pid> will send SIGKILL and will stop the process immediately.
For example:
If PID of Python process is 6243, you can use following command:
In Windows, you can use taskkill command to end the windows process. YOu can also open task manager, find python.exe and end the process. It will exit Python program immediately.
Using the raise SystemExit command
Simply put, the raise keyword’s main function is to raise an exception. The kind of error you want to raise can be defined.
BaseException class is a base class of the SystemExit function. The SystemExit function is inherited from the BaseException such that it is able to avoid getting caught by the code that catches all exception.
The SystemExit function can be raised in the following way in Python code:
6 Easy Ways to Stop Script From Execution in Python
Is it possible to stop the execution of a Python script at any line with a command? The answer is Yes. You can stop the Python script at any point in a given time. You can do it manually or programmatically; in this tutorial, we will see each approach individually.
How to stop a script in Python
There are 6 easy ways to stop a Python script.
- To stop a python script, just press Ctrl + C.
- Use the exit() function to terminate Python script execution programmatically.
- Use the sys.exit() method to stop even multi-threaded programs.
- Using an interactive script with just exit.
- You can use pkill -f name-of-the-python-script.
- Using OS._exit(0) method.
Method 1: Using Ctrl + C
To stop a script in Python, press Ctrl + C. If you are using Mac, press Ctrl + C. If you want to pause the process and put it in the background, press Ctrl + Z (at least on Linux).
To kill a script, run kill %n where “n” is the number you got next to “Stopped” when you pressed Ctrl + Z. If you want to resume it, run fg.
If your code runs at an interactive console, pressing Ctrl + C will raise the KeyboardInterrupt exception on the main thread.
If your Python code doesn’t catch that exception, then the KeyboardInterrupt will cause Python to exit. However, an except KeyboardInterrupt: block, or something like a bare except, will prevent this mechanism from truly stopping the script from running.
If KeyboardInterrupt is not working, you can send a SIGBREAK signal instead; on Windows, the interpreter may handle Ctrl + Pause/Break without generating a catchable KeyboardInterrupt exception.
Method 2: Stop script programmatically in Python
Use your code’s exit() function to stop the Python script programmatically. There is an even more ideal solution to stop the script, and you can use the sys.exit() method.
The sys.exit() function terminates the script even if you run things parallel through the multiprocessing package.
To use the sys.exit() in your program, you must import it at the start of your file.
Let’s see the following example.
If you run the program, it will not give you any output because it stops before it executes the dict code.
Method 3: Using OS._exit(0) method
You can also terminate the Python script execution using the os._exit(0) method. To use the _exit(0) method, you must import the os module at the start of the file.
It will not give you any output since the program is already terminated by the os._exit(0) function.
How to stop/terminate a python script from running?
I wrote a program in IDLE to tokenize text files and it starts to tokeniza 349 text files! How can I stop it? How can I stop a running Python program?
![]()
18 Answers 18
You can also do it if you use the exit() function in your code. More ideally, you can do sys.exit() . sys.exit() which might terminate Python even if you are running things in parallel through the multiprocessing package.
Note: In order to use the sys.exit() , you must import it: import sys
![]()
To stop your program, just press Control + C .
![]()
If your program is running at an interactive console, pressing CTRL + C will raise a KeyboardInterrupt exception on the main thread.
If your Python program doesn’t catch it, the KeyboardInterrupt will cause Python to exit. However, an except KeyboardInterrupt: block, or something like a bare except: , will prevent this mechanism from actually stopping the script from running.
Sometimes if KeyboardInterrupt is not working you can send a SIGBREAK signal instead; on Windows, CTRL + Pause/Break may be handled by the interpreter without generating a catchable KeyboardInterrupt exception.
However, these mechanisms mainly only work if the Python interpreter is running and responding to operating system events. If the Python interpreter is not responding for some reason, the most effective way is to terminate the entire operating system process that is running the interpreter. The mechanism for this varies by operating system.
In a Unix-style shell environment, you can press CTRL + Z to suspend whatever process is currently controlling the console. Once you get the shell prompt back, you can use jobs to list suspended jobs, and you can kill the first suspended job with kill %1 . (If you want to start it running again, you can continue the job in the foreground by using fg %1 ; read your shell’s manual on job control for more information.)
Alternatively, in a Unix or Unix-like environment, you can find the Python process’s PID (process identifier) and kill it by PID. Use something like ps aux | grep python to find which Python processes are running, and then use kill <pid> to send a SIGTERM signal.
The kill command on Unix sends SIGTERM by default, and a Python program can install a signal handler for SIGTERM using the signal module. In theory, any signal handler for SIGTERM should shut down the process gracefully. But sometimes if the process is stuck (for example, blocked in an uninterruptable IO sleep state), a SIGTERM signal has no effect because the process can’t even wake up to handle it.
To forcibly kill a process that isn’t responding to signals, you need to send the SIGKILL signal, sometimes referred to as kill -9 because 9 is the numeric value of the SIGKILL constant. From the command line, you can use kill -KILL <pid> (or kill -9 <pid> for short) to send a SIGKILL and stop the process running immediately.