2013-05-02 126 views
0

我试图执行脚本“test.py”,如下所示,但遇到以下错误。运行外部python脚本时出现子进程错误

import subprocess 
from subprocess import Popen, PIPE, STDOUT 
verifygerritscmd = ("python test.py --InputFile=list.txt --Project=location/code --branch=master") 
gerritPipe = Popen(verifygerritscmd, stdout=PIPE, stderr=PIPE) 
(output, error) = gerritPipe.communicate() 
if error != "": 
    print error 
    raise IOError, "gerrit command %s failed" % (gerritCmd) 
print output 

错误:

Traceback (most recent call last): 
    File "test_anoop.py", line 4, in <module> 
    gerritPipe = Popen(verifygerritscmd, stdout=PIPE, stderr=PIPE) 
    File "/usr/lib/python2.6/subprocess.py", line 633, in __init__ 
    errread, errwrite) 
    File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child 
    raise child_exception 
OSError: [Errno 2] No such file or directory 

任何想法可能是错在这里?

回答

0

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.

这意味着你需要这样做:

verifygerritscmd = ("python test.py --InputFile=list.txt --Project=location/code --branch=master").split() 

要创建ARGS的列表。或者你可以在Popen构造函数中指定shell=True。你

gerritPipe = Popen(verifygerritscmd, stdout=PIPE, stderr=PIPE, shell=True) 

还应该确保你的路径是正确的。在使用子流程模块时,最好不要对使用的路径有所疑虑。所以调用“test.py”应该改为“/path/to/test.py”

0

尝试设置参数壳=真:

gerritPipe = Popen(verifygerritscmd, stdout=PIPE, stderr=PIPE, shell=True) 

解释:没有,你试图执行一个名为脚本python test.py ...

您也可以尝试定义verifygerritscmd的列表:

verifygerritscmd = ['/usr/bin/python', 'test.py', 'argument1', 'argument2' ... ] 
0

来自docs

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

相关问题