10

Working on some code and I'm given the error when running it from the command prompt...

NameError: name 'Popen' is not defined

but I've imported both import os and import sys.

Here's part of the code

exepath = os.path.join(EXE File location is here)
exepath = '"' + os.path.normpath(exepath) + '"'
cmd = [exepath, '-el', str(el), '-n', str(z)]
print 'The python program is running this command:'
print cmd
process = Popen(cmd, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]

Am I missing something elementary? I wouldn't doubt it. Thanks!

SilentGhost
322k67 gold badges312 silver badges294 bronze badges
asked Jun 17, 2009 at 15:38
4
  • specify version of Python. some modules were changed in python-2.6 Commented Jun 17, 2009 at 15:47
  • 1
    Python 2.5 After saying... process = os.Popen(cmd, stderr=STDOUT, stdout=PIPE) it now gives me the error... NameError: name 'STDOUT' is not defined Commented Jun 17, 2009 at 15:53
  • After that.. WindowsError: [Error 3] The system cannot find the path specified Commented Jun 17, 2009 at 18:13
  • this is not a Python issue. does your exepath exist? you need to ask another question to get proper answers re this error (it has nothing to do with your original question), this question has run its course. Commented Jun 17, 2009 at 18:56

6 Answers 6

38

you should do:

import subprocess
subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
# etc.
answered Jun 17, 2009 at 15:43
Sign up to request clarification or add additional context in comments.

1 Comment

After that.. WindowsError: [Error 3] The system cannot find the path specified
7

Popen is defined in the subprocess module

import subprocess
...
subprocess.Popen(...)

Or:

from subprocess import Popen
Popen(...)
answered Jun 17, 2009 at 15:41

3 Comments

from subprocess import Popen; Popen(...)
After saying... process = os.Popen(cmd, stderr=STDOUT, stdout=PIPE) it now gives me the error... NameError: name 'STDOUT' is not defined
process = subprocess.Popen(cmd,stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
2

When you import a module, the module's members don't become part of the global namespace: you still have to prefix them with modulename.. So, you have to say

import os
process = os.popen(command, mode, bufsize)

Alternatively, you can use the from module import names syntax to import things into the global namespace:

from os import popen # Or, from os import * to import everything
process = popen(command, mode, bufsize)
answered Jun 17, 2009 at 15:43

Comments

1

This looks like Popen from the subprocess module (python>= 2.4)

from subprocess import Popen
answered Jun 17, 2009 at 15:43

Comments

1

If your import looks like this:

import os

Then you need to reference the things included in os like this:

os.popen()

If you dont want to do that, you can change your import to look like this:

from os import *

Which is not recommended because it can lead to namespace ambiguities (things in your code conflicting with things imported elsewhere.) You could also just do:

from os import popen

Which is more explicit and easier to read than from os import *

S.Lott
393k83 gold badges521 silver badges791 bronze badges
answered Jun 17, 2009 at 15:42

Comments

-2

You should be using os.popen() if you simply import os.

answered Jun 17, 2009 at 15:40

2 Comments

I also imported subprocess if that helps clarify anything
You're invoking "Popen()" when in fact you should be using "os.popen()"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.