subprocess.check_output 失败,错误 127 [英] subprocess.check_output failing with error 127

查看:108
本文介绍了subprocess.check_output 失败,错误 127的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的 python 应用程序调用外部程序,但它没有显示任何输出并且失败并显示错误 127.从命令行执行命令工作正常.(我在正确的工作目录中)

I'm attempting to call an outside program from my python application, but it shows no output and fails with error 127. Executing the command from the command line works fine. (and I am in the correct working directory)

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.它是正确的,但很长,所以我在这个例子中省略了它)

Each loop outputs the following error: (I've also checked e.cmd. It's correct, but long, so I omitted it in this example)

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()

推荐答案

尝试在 subprocess.check_output() 调用中添加 casperjs 的完整路径.

Try adding the full path to casperjs in your subprocess.check_output() call.

回答你的第二个问题.对于我在 iPad 上的格式,我深表歉意.我认为您应该尝试 Popen 而不是 check_output 以便您可以指定环境变量:

Answeing your 2nd question. My apologies for the formatting as I'm on iPad. I think you should try Popen instead of check_output so that you can specify environment variables:

p = subprocess.Popen(["/path/to/casperjs", "casper.js", startUrl, row[0]], env={"PATH": "/path/to/phantomjs"})
url, err = p.communicate()

这篇关于subprocess.check_output 失败,错误 127的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆