3
votes

i get error while plainly using STDOUT

>>> import subprocess
>>>print STDOUT

Traceback (most recent call last): File "", line 1, in NameError: name 'STDOUT' is not defined

it also works with

from subprocess import STDOUT

But what if there are many such constants in the module, is there a way to import any such constants defined in a module without mentioning them explicitly.

1
while using subprocess.STDOUT it works >>>print subprocess.STDOUT -2 >>>> - ravi.zombie

1 Answers

7
votes

You need to tell Python where to find "STDOUT", i.e. in the 'subprocess' module. That's why when you specify "subprocess.STDOUT" it works. If you want to be able to refer to STDOUT without always having to name the module, import it like this:

from subprocess import STDOUT

or, if you are using all of the functions and classes from subprocess, you can import them all like this

from subprocess import *

but it is recommended you avoid this whenever possible for a lot of good reasons (see What exactly does "import *" import?). Otherwise, you should probably just import all of the methods and classes you will use as a tuple in the import statement:

from subprocess import STDOUT, popen, call