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.

Running Powershell Script Within Python Script

You can run a PowerShell script in Python using the subprocess or os module. In this article, we will learn how to execute a PowerShell script from Python in Windows, Mac, or Linux. The fundamental prerequisite is to have PowerShell installed on your system.

Windows comes preinstalled with PowerShell (you can update it by downloading the latest version from this link). You can install PowerShell core on Linux and Mac using the information below.

Installing PowerShell core in Linux

You can install PowerShell core on Debian by running the following commands from the terminal (source: Microsoft website). If you are running a different distribution, you can get the installation commands from here.

Installing PowerShell Core on Mac

You can install PowerShell using Homebrew by running the following command on the terminal.

Or update it using the commands

Once done with the installation, you can start PowerShell from Windows, Linux, or Mac terminal by running this command in the terminal

And execute a PowerShell script using the command

At this point, we are ready to execute a PowerShell script from within a Python script.

Running PowerShell Script Within Python Script

As an example, we will execute the PowerShell script named script1.ps1 with the content below. The script accepts one parameter, $Name, and prints a greeting message to the console based on the time of the day.

Output:

The time is 04/14/2023 18:46:50
Good afternoon, Decker!

If you want to save the output of the PowerShell script to a variable, you can send the output to the subprocess.PIPE, as shown below.

Output:

The time is 04/15/2023 17:37:35
Good afternoon, Decker!

You can also run the PowerShell script using the os module as follows.

If the execution runs successfully with no error, return_code=0. Note that os. system() has limited functionalities. The subprocess module discussed above provides more facilities for spawning new processes.

Conclusion

This guide discussed using the subprocess module to run the PowerShell script using Python across different platforms – Windows, Linux, and Mac.