我正在尝试从我的 python 应用程序调用外部程序,但它没有显示任何输出并且失败并出现错误 127。从命令行执行命令工作正常。(我在正确的工作目录中)
def buildContris (self, startUrl, reportArray):
urls = []
for row in reportArray:
try:
url = subprocess.check_output(["casperjs", "casper.js", startUrl, row[0]], shell=True)
print (url)
urls.append(url)
break
except subprocess.CalledProcessError as e:
print ("Error: " + str(e.returncode) + " Output:" + e.output.decode())
return urls
每个循环输出以下错误:(我也检查了e.cmd。它是正确的,但很长,所以我在这个例子中省略了它)
Error: 127 Output:
解决方案:
以下代码有效
app = subprocess.Popen(["./casperjs/bin/casperjs", "casper.js", startUrl, row[0]], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env = {"PATH" : "/usr/local/bin/:/usr/bin"}, universal_newlines=True)
app.wait()
out, errs = app.communicate()