In a computer, everything that occurs is a process, to put it simply. For example, you begin a new process each time you launch an application, issue a command-line command, or execute a Python script. Likewise, everything on your computer, from displaying a menu bar to launching a complicated application, is a process.
For instance, when you run a Python script from the command line, a new process is started. There is a relationship between these two processes: the process that creates another is the parent, and the newly generated process is the child.
Similarly, a Python script can start a new process, at which point it takes on the role of the new process parent. In this post, we’ll look at how to launch various subprocesses within a standard Python script using the Python subprocess module.
Although this is a simple post, it may help to have a basic understanding of Python to follow along with the examples and concepts.
Python Subprocess Module
A common Python module called the subprocess allows you to launch new processes within a Python script. When you need to execute numerous processes simultaneously or call an external program or command from inside your Python code, it’s beneficial and the option that is advised.
The ability to manage inputs, outputs, and even errors generated by the child process from the Python code is one of the advantages of the subprocess module. In addition, this option increases the power and flexibility of calling subprocesses by allowing, for example, the use of the subprocess output as a variable throughout the remainder of the Python script.
A common Python module called the subprocess allows you to launch new processes within a Python script. When you need to execute numerous processes simultaneously or call an external program or command from inside your Python code, it’s beneficial, and the option is advised.
The ability to manage inputs, outputs, and even errors generated by the child process from the Python code is one of the advantages of the subprocess module. In addition, this option increases the power and flexibility of calling subprocesses by allowing, for example, the use of the subprocess output as a variable throughout the remainder of the Python script.
Use of subprocess.run() in Python
It’s easy to run commands in the background with Python’s run function using the subprocess module rather than worrying about starting a new terminal or typing the command by hand. Using this function to automate processes or run commands you don’t want to carry out manually is also an excellent idea.
The iterable *args, which contains the instructions to run the subprocess, is the significant parameter given to the function. In addition, the paths to the Python executable and the Python script should be included in the args to execute other Python code.
Thus, it would appear as follows:
import subprocess subprocess.run(["python", "TechVidvan.py"])
Instead of supplying a .py file, Python code can alternatively be directly entered into the procedure. An example of using such a subprocess is as follows:
result = subprocess.run(["/usr/local/bin/python", "-c", "print('Subprocess with TechVidvan')"])
We have the following in args:
Python’s local executable can be found at “/usr/local/bin/python” in the path.
Python’s “-c” tag enables users to enter Python code as text on the command line.
The actual code to be executed is “print(‘Subprocess with TechVidvan’)”.
This is equivalent to running the command line with the argument /usr/local/bin/python -c print(‘Subprocess with TechVidvan’). This structure will be used for most of the code in this article because it makes it simpler to demonstrate the run function’s features. But, of course, you could always call another Python script that uses the same code.
Additionally, the first string in args in both of the cases above where we used the run function refers to the location of the Python executable. We’ll maintain the generic path used in the first example for the rest of the essay.
The sys module can help you if you’re having problems determining the location of the Python executable on your computer. This module works well with the subprocess, and a good application for it is to change the executable’s path to look like this:
import sys
result = subprocess.run([sys.executable, "-c", "print('Subprocess with TechVidvan')"])
The completed process is represented by an object of the CompletedProcess class, which the run function then returns.
If we print the results, they will appear as follows:
CompletedProcess(args=['/usr/bin/python3', '-c', "print('Subprocess with TechVidvan')"], returncode=0)
As we would anticipate, a CompletedProcess class instance displays the command and the returncode=0, signifying that it was executed correctly.
Parameters For Subprocess Module
We should look into options that allow us to use the run function better now that we know how to launch a subprocess.
Controlling the Result in Subprocess Module
Remember that the object returned by the aforementioned function displays the command and the return code with no more details regarding the subprocess. The user has more control over their code if the capture output argument is set to True, which returns more data.
Input:
result = subprocess.run(["python", "-c", "print('Subprocess with TechVidvan')"], capture_output=True)
Output:
Both are byte sequences representing the outputs of the respective subprocess. To have these outputs as strings, we can, alternatively, set the text argument to True.
However, if your script makes a mistake, stderr will have the following error message, and stdout will be empty:
result = subprocess.run(["python", "-c", "print(subprocess)"], capture_output=True, text=True)
print('output: ', result.stdout)
print('error: ', result.stderr)
Output:
You can now use the subprocesses’ output as a conditional variable for the rest of your code, a constant variable, or even to keep track of the subprocesses and save them (assuming all are completed without issues) and their outputs.
The Input in the Subprocess Module
As we previously saw, it is possible to use a child process’ output throughout the rest of the parent code. However, the contrary is also true: by utilizing the input parameter, we may send a value from the parent process to the child process.
If text=True, we utilize this argument to communicate any string or byte sequences to the subprocess, which will receive this data via the sys module. The input parameter will be read by the sys.stdin.read() function in the child process, where it can then be assigned to a variable and utilized in the code like any other variable.
Here’s an example:
Input:
result = subprocess.run(["python", "-c", "import sys; my_input=sys.stdin.read(); print(my_input)"], capture_output=True, text=True, input='TechVidvan') print(result.stdout)
Output:
The code in the subprocess above imports the module, assigns the input to a variable using sys.stdin.read(), and outputs that variable.
However, we can also input data using args and read them inside the child code using sys.argv. Let’s take the following code from a script called my script.py as an example:
import sys
my_input = sys.argv
def sum_two_values(a=int(my_input[1]), b=int(my_input[2])):
return a + b
if __name__=="__main__":
print(sum_two_values())
The run function below contains two additional values that will be retrieved by sys.argv and added to my script.py. They are contained in the script that will serve as the parent process for the child process above.
Input:
result = subprocess.run(["python", "my_script.py", "2", "4"], capture_output=True, text=True) print(result.stdout)
Output:
6
Conclusion:
A sub-process is an artificially produced computer process. Tools like task manager and htop allow us to monitor the active processes on our computer. In addition, Python’s subprocess library is available. Currently, the run function provides a straightforward interface for setting up and controlling subprocesses.
Because we connect directly with the OS, we can construct any application with them. Finally, remember that creating something you would like to use is the best way to learn.

