Reputation: 41
I'm trying to run one python program from another using subprocess
. Here's the function I've got so far:
def runProcess(exe):
p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while(True):
retcode = p.poll() #returns None while subprocess is running
line = p.stdout.readline()
yield line
if(retcode is not None):
break
then i run:
for line in runProcess('python myotherprogram.py'): print line
but I get an OS error: no such file
, but it doesn't tell me what file doesn't exist. It's baffling. Any suggestions? I can use the runProcess
function for normal terminal commands, such as ls
.
Upvotes: 0
Views: 68
Reputation: 155416
What doesn't exist is a single executable named python myotherprogram.py
. To specify arguments, you need to provide a list consisting of the command and its argument, such as with runProcess(["python", "myotherprogram.py"])
, or specify shell=True
to the Popen
constructor.
The relevant quote from the documentation:
args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.
On Unix, if args is a string, the string is interpreted as the name or path of the program to execute. However, this can only be done if not passing arguments to the program.
Upvotes: 4