I'm using Python 3.5.1 and Windows 10. I can't get subprocess to put output to the screen. So let's start with something simple:
import subprocess
process = subprocess.run('echo hi', shell=True, stdout=subprocess.PIPE)
When I run my python module, I want to it to print 'hi' in the Python Shell. The script runs and does not return an error, but it prints nothing to the screen.
I have also tried many different flavors of subprocess (i.e. Popen), but still no luck. I have a feeling this has something to do with the way my Windows/Python environment is setup, but I don't really know where to start. Thoughts?
Update
So I now understand that my original code sample should not have put anything on the screen; however, when I run
import subprocess
p = subprocess.run('echo hi', shell=True)
I get no output. When I run:
import subprocess
p = subprocess.run('echo hi', shell=True, stdout=subprocess.PIPE)
print(p.stdout)
I do get output. Why did the first example not work?
subprocessto route standard output to a pipe instead of to the screen.PIPEis so that you can communicate with the process via python file-like objects, and also so use different pipes for different processes. For instance, to prevent multiple processes from all writing to stdout at the same time. The pipes are created on the.stdin,.stdout, and.stderrattributes on the subprocess objectstdoutis (on Windows) a file handle. In a console process it "points" to the console, just as a different file handle might "point" to a conventional file. By default, when you create a new process from a Windows console thestdoutis copied to the new process. The console is shared by the new process as well. Both these actions can be overridden. No shell need be involved! On Windows a console does not need a shell.python.exeis a console program, it depends how it is launched as to which console is used.