Python

Python Exit Codes

If you have ever worked on a Unix system, you are probably familiar with the concept of an exit code. If you are not, let’s do a quick recap. An exit code refers to a specific exit code returned by a command, script, or program upon execution. A unique code is used to indicate the status of the process after it has completed execution. The status of the process could be successful, failure, or other condition. Therefore, the role of exit codes is to indicate how the process behaved.

With that out of the way, let us explore Python exit codes, how they work, and how you can use them in your programs.

Python Standard Exit Codes

Python has only two standard codes, i.e., a one and a zero. The exit code of 0 means that the process has been executed and exited successfully. This means no error was encountered. On the other hand, an error code of 1 indicates that the process exited with a failure. Let us take a look at a very simple program that just prints the string “Welcome to linuxhint!”.

print("welcome to linuxhint!")

As you can guess, if we run the program above, it will execute successfully.

$ python exit_codes.py
welcome to linuxhint!

We can see that the program does return the desired output. On Unix systems, we can use the echo command followed by the environment variable ?. This environment variable allows you to get the exit code of the last executed command.

In our case, the last command is the Python program:

$ echo $?

0

Notice that the command above returns 0. This shows that our Python program executes successfully.

Have you ever customized your shell, and it returns a red symbol if the previous command fails? Yes, it uses the exit code to do this.

Now let’s take another example:

print("welcome to linuxhint!"

Notice something wrong with the program above?

In the above example, the code above is missing the closing bracket for the print function. Hence, if we run the program above, it will fail and return an error.

$ python exit_codes.py

File "/Users/username/exit_codes.py", line 2

^

SyntaxError: unexpected EOF while parsing

Here, we can clearly see that the program fails and returns an error.

Let’s now check the exit code.

echo $?

1

You guessed it; the command returns an exit code 1. This shows that the previous program failed miserably.

Python Custom Exit Codes – The SYS Module

One of the most useful modules that I encourage fellow Python geeks to learn is the SYS module.

It is a great module that is built-in Python’s standard library but provides exceptional system functionalities.

Enough praises; we can use one of the functions from the SYS module to create custom exit codes in our Python programs.

The function is called exit() and has syntax as the one shown below:

sys.exit([arg])

As you can tell, the function has a relatively simple syntax.

It allows you to specify the arg parameter, which is optional. It can be an integer or any supported object.

If the provided argument is an integer zero or None, it is considered a successful execution. For any other value above zero, it indicates abnormal termination.

Although it will depend on the system, the value of the exit code can range from 0 -127.

You can also pass a string which will be displayed when the program exits.

Take the example program below that returns an exit code of 125.

from sys import exit

print("Hi!")

exit(125)

print("Welcome to linuxhint")

In the example program above, we start by importing the exit function from the sys module.

We then use the print statement to print some text on the screen.

For the important part, we use the exit function and pass the exit code as 125.

NOTE: As soon as Python encounters the exit() function, it will immediately terminate the program and return the exit code or message specified.

We can verify this by running the program as:

$ python3 exit_codes.py

Hi!

We can see from the above output that the second print statement does not execute.

Let’s check the exit code as:

$ echo $?

125

We can see that the exit code is 125, as specified in our program.

Consider the example below that prints a string instead of an exit code.

from sys import exit

print("Hi!")

exit("Program terminated unexpectedly")

print("Welcome to linuxhint")

In this case, we are using a string literal as the arg parameter inside the function. Once we run the program:

$ python3 exit_codes.py

Hi!

Program terminated unexpectedly

We can see that the program prints the message before the exit function and the one in the exit function.

Can you guess what the exit code of the program above is? Let’s see:

$ echo $?

1

Yes, it’s one. Any abnormal terminations of the program that do not provide their own custom codes will be assigned an exit code of 1.

NOTE: Use the exit function when you need to terminate your program immediately.

Conclusion

And with that, we have come to the end of our tutorial. In this article, we learned how Python exit codes work and how to create custom exit codes using the sys module.

Happy coding, and as always, Thanks for reading!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list