Author Archives: Kiprono Koech

Process Finished With Exit Code 0 in Python

Python interpreter generates exit codes at the end of the execution to indicate if the code was executed successfully with no errors/exceptions.

The exit code 0 is the standard code used to indicate that our program is executed successfully with no errors. Any non-zero exit code is used for executions that terminated with errors or exceptions.

How to Read Python Exit Codes

Method 1: In some cases, exit codes are printed on the output console

Some IDEs and code editors, such as PyCharm and Sublime Text, print the exit code on the terminal once execution is done. For example,

When the above code is executed on Pycharm, the following content is printed on the console.

Next, let’s execute code that leads to an error in Pycharm.

Since the above code yielded a ZeroDivisionError, the execution was terminated with exit code 1.

Method 2: Running Python script on the terminal, then check the returned code

This is done by executing our script on the command line and then checking the value returned by the Python interpreter after the execution.

Example

First, save the following code in a script called exit_code1.py.

Then you can execute it in the terminal with the following command

Lastly, check the code by running (for Unix users)

Or use the following command (Windows Users):

The sequence “$?” for UNIX systems and “%errorlevel%” for Windows contains the exit code of the last command.

And, if we run code with errors, we get a non-zero exit code, as shown in this example (the code is saved on the exit_codetf.py file).

Terminating Python Execution using Custom Exit Codes

The standard exit codes for Python are 0 and 1 – 0 for successful execution and 1 for abnormal termination. However, you can define a different exit code using the sys.exit(code) function.


Note: sys.exit() function accepts 8-bit exit codes, that is, any value between 0 and 255. If you provide a value outside that range, it’s treated as the modulo 256 of the given number.

For example, if you issue -4, sys.exit() will use 252 as the exit code, and if you supply 447, 191 will be used, as shown below.

Output:

252
191
135

Let’s see how to supply a custom exit code using sys.exit(). In this example, we take an examination score and check if it is negative, between 0 and 100, or above 100. If the supplied mark is less than 0, we issue an exit code of 13, and if the score given is greater than 100, we exit with the code 101.

Output (first take with a negative mark):

Enter the score: -33
Oops! You entered a negative mark!
Process finished with exit code 13

Output (second take with a score between 0 and 100):

Enter the score: 68
You entered 68
Process finished with exit code 0
Output (third take with a score above 100):
Enter the score: 206
Oops! You entered a value greater than 100!
Process finished with exit code 101

Conclusion

There are two standard exit codes in Python – 0 and 1. The former means the program exited normally – without errors, but the latter means the program terminated with errors.

This article discussed how to check the exit code and set custom codes in Python. After going through the guide, you should easily interpret exit codes in Python.