Python – Enable printing of subprocess.run in spyder ipython

Enable printing of subprocess.run in spyder ipython… here is a solution to the problem.

Enable printing of subprocess.run in spyder ipython

I’m running spyder on Windows 10 and when I try to run a command similar to the following:

cmd = 'python /path/to/program.py arg1 arg2'
subprocess.run(cmd,shell=True)

The script is running as expected, but I would like to see what the command executed in the spyder ipython console prints on the screen. I know the program is printing content to the screen the other way (running the program from the shell), so the script I’m running has no errors.

How do I enable printing for a sub-process?

Solution

The output comes from a stream named stdout. To capture it, you need to redirect it to the pipeline, which then terminates in the calling process. subprocess.run(...) Built-in support to handle this:

import subprocess
cmd = 'python /path/to/program.py arg1 arg2'.split()
proc = subprocess.run(cmd, stdout=subprocess. PIPE, universal_newlines=True)
print(proc.stdout)

As you can see, the output is captured in a CompletedProcess object (proc) and then accessed as member data. Also, to turn the output into text (a string) instead of bytearray, I passed the parameter universal_newlines=True.

However, it should be noted that subprocess.run(...) finishes running before returning control. Therefore, this does not allow the output to be captured “in real time”, but rather after the whole process is over. If you want real-time capture, you have to use subprocess instead. Popen(...) Then use .communicate() or some other means of communication to capture the coming from the child process.

Another comment I’d like to make is that shell=True is deprecated. Especially when dealing with unknown or untrusted input. It leaves the interpretation of cmd to the shell, which leads to various security vulnerabilities and bad behavior. Instead, split cmd into a list (e.g., as I did), then pass that list to subprocess.run(...) and omit shell=yes.

Related Problems and Solutions