
#Python subprocess get output code
The code variable is a multi-line Python string and we assign it as input to the n command using the input option. Completely useless, but (hopefully) very instructive! We just used Python to execute some Python code with the python3 binary. > result = n(, input=code, capture_output=True, encoding='UTF-8') We’ll build on the previous examples here: > import subprocess Please note that I’m not going into streaming data here. If the external command expects data on standard input, we can do so easily as well with the input option of Python’s n function. I’d recommend specifying the encoding explicitly if you know it. Hence, if you know the output will be ASCII text or UTF-8 text, you’re better off specifying it so the run function encodes the captured output accordingly as well.Īlternatively, you can also use the option text=True without specifying the encoding. As a result, stdout and stderr will be byte arrays. If you don’t, n assumes the output is a stream of bytes because it doesn’t have this information. I also added the option encoding=’UTF-8′. Since there were no errors, stderr is empty. After inspecting the result variable, we see that the Python version was captured from standard out. The n command redirected the standard out and standard error streams so it could capture them and store the result for us. > result = n(, capture_output=True, encoding='UTF-8')ĬompletedProcess(args=, returncode=0, stdout='Python 3.8.5\n', stderr='')Īs you can see, Python didn’t print its version to our terminal this time. We can achieve this with the capture_output=True option: > import subprocess If you run an external command, you’ll likely want to capture the output of that command. In this case, make sure the Python binary is called python3 on your system too, and that it’s in the PATH. Perhaps, you’ll even get an error looking like this: FileNotFoundError: No such file or directory: 'python3'. Your result may vary because your Python version will likely be different. It depends on the process you called what the different return code means.Īs you can see in the output, the Python binary printed its version number on standard out, which is usually your terminal.

Any other return code would mean there was some kind of error. The process returned code 0, meaning it was executed successfully.


#Python subprocess get output install
Since it is part of Python 3, you don’t need to install it separately. Create a Python subprocess with nĮnough with the theory, it’s time to get our hands dirty and write some code to execute external commands.įirst of all, you need to import the subprocess library. I thought it would be nice for you to know what’s going on internally, but if you feel confused, rest assured that you don’t need this knowledge to do what you want: running an external command with the Python subprocess module. This wrapper is the function run() from the subprocess package and that’s what we’ll use in this article. Thanks to the wrapper, running an external command comes down to calling a function.

We can go the low-level way and do much of this ourselves using the Python subprocess module, but luckily, Python also offers a wrapper that will take care of all the nitty-gritty details, and do so safely too. That copy, in turn, replaces itself with another process: the process you were looking to execute. First, the process forks itself, creating a copy. We can utilize this same technique to start another process, though. This can be useful if you want to parallelize your code and utilize multiple CPUs on your machine. The process forks itself, meaning a new copy of the process is created and started. What happens internally (inside the OS kernel) is what’s called a fork. A parent process spawning two sub-processes
