Python is one of the most versatile programming languages on the planet. You can use it to develop a wide range of solutions. Examples include web applications, Machine Learning, Artificial Intelligence, and Data Science. Also, Python is the most used programming language. Thanks to user-friendly syntax and powerful features. It’s very easy to use. You can exit a Python script whenever you want. But how can you do it? In this article, you will know how to exit a Python script. You will find different ways to easily exit program in Python. Let’s dive in.
Table of Contents
How to exit a Python script using the quit() function
You can exit a Python program by using the built-in quit() function. It’s very easy to use. It doesn’t require you to import any external libraries.
quit() has a very simple syntax. Let’s take a look at it:
1 |
quit() |
When the system comes up against the quit() function, it completely concludes the execution of the program.
The quit() function works only if the site module is imported. Therefore, you shouldn’t use it in production code. Because it is not suitable for use in the application code for the intended audience in a real-world situation. You should use the quit() function only in the interpreter. It raises the SystemExit exception.
Read: How To Run A Python Script?
To understand the quit() function, create a Python file and add the following script:
1 2 3 4 5 6 7 8 9 10 11 12 |
for value in range(0,10): # If the value becomes 6 then the program prints quit # message and terminates via quit() if value == 6: # Prints the quit message print(quit) quit() # Values get printed till the program terminates print(value) |
Now, run the code. You will see this output:
1 2 3 4 5 6 |
0 1 2 3 4 5 |
How to exit a Python Script using the sys.exit() function
You can import the sys module to your Python code. It provides various variables and functions. You can utilize them to manipulate different pieces of the Python runtime environment.
The sys.exit() function is an in-built function of the sys module. You can use it to exit the Python program.
The sys.exit function accepts an optional argument. You can utilize it to output an error message. Its default value is 0, which indicates successful termination.
You can use sys.exit() function at any time to exit the execution process. You won’t face any issues.
Read: This Is How To Check Python Code
To use the method, you have to import sys. Then you need to call the exit() method on the sys object.
You can utilize the sys.exit() function in your Python program in the following way:
1 2 3 4 5 6 |
import sys a = 5 if a != 12: sys.exit("Values don't match unfortunately") else: print("They match") |
You will get this output:
1 |
Values don’t match unfortunately |
How to exit a Python script using the exit() function
exit() function is another great option. It enables you to effortlessly end your Python program.
exit() is an alternative to the quit() function. It can make the code a bit more user-friendly. However, you should use it only in the interpreter.
You can utilize the exit() function in your Python program in the following way:
1 2 3 |
for a in range(1,5): print(a+2) exit() |
You will get this output:
1 |
3 |
As you can see, sys.exit() is very easy to use. That’s why it has become one of the most popular methods to terminate a Python program.
You can implement both exit() and quit() functions only when you import the site module. However, they cannot be used in the production and operational codes.
How to exit a Python script using the KeyboardInterrupt command
Is your Python program running in the console? Try pressing CTRL + C on Windows or CTRL + Z on Unix. You will find that an exception has been raised in the main thread. It’s called KeyboardInterrupt.
What happens if the Python program doesn’t catch the exception? It will exit the script. You need to utilize except: for catching this exception. It may prevent Python program from exiting.
If KeyboardInterrupt doesn’t work for you, then you can consider using the SIGBREAK signal. You just need to press CTRL + PAUSE/BREAK on Windows.
In Linux/Unix, you can find the PID of the Python process by using this command:
1 |
ps -ef|grep python |
To kill the python process, you need to utilize the kill -9 command.
Example:
If the PID of the Python process is 6243, you can kill it by utilizing this command:
1 |
kill -9 6243 |
If you are using Windows, you can use the taskkill command to end the process. Also, there are alternative ways. For example, you can open task manager, find python.exe, and end the process.
Read: A Beginner’s Guide To Python Tools
How to exit a Python script using Raise SystemExit
The main functionality of the raise keyword is to raise an exception. It lets you define the kind of error you want to raise.
The SystemExit function is inherited from BaseException. It is designed to avoid getting caught by the code that catches all exceptions.
SystemExit is nothing but an exception. It is raised by some of the exit functions in Python code.
The quit() and sys.exit() functions can raise the SystemExit exception. It will terminate the Python program.
Here is an example of raising the SystemExit function in Python:
1 2 3 4 5 6 7 8 9 10 |
import time try: x=0 while 1==1: x=x+2 print(x) time.sleep(1) except KeyboardInterrupt: print("Raising SystemExit") raise SystemExit |
Output:
1 2 3 4 5 |
2 4 6 ^C Raising SystemExit |
How to exit a Python script using the os._exit() function
The os module is a part of Python’s standard utility modules. It provides functions for conveniently interacting with the operating system. It provides a portable way of utilizing operating system-reliant functionality.
You can utilize the os._exit() method to exit the process with the specified status. There is no need to call cleanup handlers or flushing stdio buffers.
You can utilize the os._exit() method after importing the os module in your Python code.
It is generally used in the child process after the os.fork() system call occurs.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import os pid = os.fork() if pid > 0: print("nThis is the parent process") info = os.waitpid(pid, 0) if os.WIFEXITED(info[1]) : code = os.WEXITSTATUS(info[1]) print("Child's exit code:", code) else: print("Now in the child process") print("The Process ID is:", os.getpid()) print("Child is now exiting") os._exit(os.EX_OK) |
Output:
1 2 3 4 5 |
This is the parent process Now in the child process The Process ID is: 294 The Child will now exit Child’s exit code: 0 |
Conclusion
In this post, you have learned different ways of exiting Python scripts. All of them are very simple. Hence, you can effortlessly implement the methods. Most people prefer to use the sys.exit() function. Because you cannot utilize the exit() and quit() functions in production code. For special use cases, you can use os._exit(). It enables you to immediately exit your Python program.
FAQ
How do you exit a Python script in the terminal?
If you are using Linux and macOS, simply press Ctrl + D. The interpreter will instantly end. Pressing Enter isn’t necessary.
How do I exit Python Main?
You can use sys. exit(). It enables you to exit from the middle of the main function.
How do I exit a Python window?
To terminate an interactive Python session, you need to call the exit() function. Alternatively, you can press Ctrl + Z.
What is the sys module?
The sys module in Python provides various functions and variables. You can utilize them to manipulate different parts of the Python runtime environment. It provides access to the variables and functions that interact strongly with the interpreter. It allows you to operate on the interpreter.