Process Finished With Exit Code 0 in Python

When working with Python, you may have come across the message “Process finished with exit code 0.” This message is a notification that your program has been successfully executed and completed without errors. If you want your Python code to run smoothly and efficiently, you need to understand what this message means.

This tutorial will deliver an in-depth guide on the “Exit Code 0”. Let’s start with the following contents:

  • What is an Exit Code 0 in Python?
  • How Does “Process Finished With Exit Code 0” Occurs?
    • Successful Completion of the Program
    • Pressing Ctrl+C / Closing the Window (Using Thonny Interpreter)
    • Using sys.exit(0) Function

What is an Exit Code 0 in Python?

In Python, Exit Code 0 is returned when the program has successfully executed and completed without errors. This means the program has run as expected and has not encountered any issues during execution. 

Note: Whenever our code encounters an error, exit code 1 is displayed, but when exit code 0 is displayed, there has been no error, and our code has run properly.

How Does “Process Finished With Exit Code 0” Occurs?

In Python, Exit Code 0 can occur for several reasons. Let’s discuss one by one using appropriate examples:

Successful Completion of the Program (PyCharm Interpreter)

The most common reason for receiving Exit Code 0 is the successful completion of the program. It indicates that the program has completed all its tasks as expected. Here is an example:

Code:

print('Hello World')

The string value is passed inside the print() function.

Output:

Note: You might see exit code 0 in PyCharm but not in Thonny or Visual Studio Code because of the different configurations of these IDEs.

Pressing Ctrl+C / Closing the Window (Thonny Interpreter)

The program will exit with Exit Code 0 if the user interrupts the execution of the program by pressing Ctrl+C or closing the window. Here is an example snippet:

Using sys.exit(0) Function (Thonny Interpreter)

When the “sys.exit(0)” function is called in the code, the program exits with Exit Code 0. Here is an example code:

Code: 

import sys
print("Python Guide")
sys.exit(0)

The sys module function named “sys.exit()” is used at the end of the code and returns the message “process ended with exit code 0” to ensure that the program has executed without any error.

Output:

Conclusion

A Python program returns Exit Code 0 if it has successfully executed and completed without errors. This enabled the program to run smoothly. If our code contains an error, exit code 1 is returned, but if there is no error, exit code 0 is returned. The exit code 0 can also be generated using the sys.exit() function of the sys module at the end of the program. This post presented an in-depth guide on the “Exit code 0” using numerous examples.