Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popen and python

Tags:

python

popen

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!

like image 925
Tyler Avatar asked Jun 17 '09 15:06

Tyler


People also ask

What is Popen in Python?

Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as in open() function.

Is Popen blocking Python?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.

What does subprocess Popen do in Python?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

Is subprocess built in Python?

The subprocess module present in Python(both 2. x and 3. x) is used to run new applications or programs through Python code by creating new processes.


2 Answers

you should do:

import subprocess
subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
# etc.
like image 90
SilentGhost Avatar answered Nov 16 '22 03:11

SilentGhost


Popen is defined in the subprocess module

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

Or:

from subprocess import Popen
Popen(...)
like image 44
David Cournapeau Avatar answered Nov 16 '22 02:11

David Cournapeau