4

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?

8
  • 1
    The reason the output is not going to the screen is that you've told subprocess to route standard output to a pipe instead of to the screen. Commented Jul 15, 2016 at 18:12
  • Maybe I don't understand the pipe then. According to the docs, subprocess.pipe is a "Special value that can be used as the stdin, stdout or stderr argument to Popen and indicates that a pipe to the standard stream should be opened." So where is the output being piped to? Commented Jul 15, 2016 at 18:18
  • @David The purpose of using PIPE is 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 .stderr attributes on the subprocess object Commented Jul 15, 2016 at 18:29
  • So when I run the process without the pipe argument, the output is going to the normal stdout? Which is obviously not the screen in my setup? Commented Jul 15, 2016 at 18:34
  • 1
    stdout is (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 the stdout is 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.exe is a console program, it depends how it is launched as to which console is used. Commented Jul 15, 2016 at 19:26

1 Answer 1

7

It's not printing to the screen because you're redirecting the output to a PIPE. Remove the pipe argument and the output will go to the normal stdout

You can use subprocess.call also.

subprocess.call('echo hi', shell=True)

If you want to pipe the output, you should use Popen instead

process = subprocess.Popen('echo hi', shell=True, stdout=subprocess.PIPE)
process.wait()
print process.stdout.read()

If you want to decode the result as UTF-8 format, just add .decode('UTF-8')

process.stdout.read().decode('utf-8')
Sign up to request clarification or add additional context in comments.

3 Comments

When I remove the pipe argument and run my code again, I get no output to the screen. Using your script with Popen, I do get output. Where is it getting piped to? And why doesn't it print to the screen if I remove the pipe argument?
No it doesn't. I'm also using Python 3.5, and I believe subprocess.call is just a legacy feature that calls run anyway?
@David, in the question you say want it to output to "Python Shell". Are you referring to IDLE? If so, you need to first attach pythonw.exe to a console. Otherwise Windows creates a console automatically, which gets destroyed as soon as cmd.exe exits since no other process is attached to the console. The following allocates and attaches to a console: import ctypes; ctypes.WinDLL('kernel32').AllocConsole().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.