Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

name 'STDOUT' is not defined though subprocess is imported

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.

like image 541
ravi.zombie Avatar asked Oct 24 '25 04:10

ravi.zombie


1 Answers

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
like image 114
Lgiro Avatar answered Oct 27 '25 01:10

Lgiro