Python子进程调用返回“command not found",终端正确执行 [英] Python subprocess call returns "command not found", Terminal executes correctly

查看:252
本文介绍了Python子进程调用返回“command not found",终端正确执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 python 运行 gphoto2,但是没有成功.它只是返回未找到的命令.gphoto 已正确安装,如终端中的命令可以正常工作.

I am trying to run gphoto2 from python but, with no succes. It just returns command not found. gphoto is installed correctly, as in, the commands work fine in Terminal.

p = subprocess.Popen(['gphoto2'], shell=True, stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT, executable='/bin/bash')

for line in p.stdout.readlines():
    print line
p.wait()

/bin/bash: gphoto2: command not found

我知道 osx 终端(应用程序)有一些有趣的地方,但是我对 osx 的了解很少.

I know that there is something funny about the osx Terminal (app) but, my knowledge on osx is meager.

对这个有什么想法吗?

编辑

更改了我的一些代码,出现其他错误

changed some of my code, other errors appear

p = subprocess.Popen(['gphoto2'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout:
    print line


    raise child_exception
OSError: [Errno 2] No such file or directory

编辑

使用完整路径'/opt/local/bin/gphoto2'

using full path '/opt/local/bin/gphoto2'

但是如果有人愿意解释使用哪个 shell 或如何登录并能够拥有相同的功能..?

but if someone care to explain which shell to use or how to log in and be able to have the same functionality..?

推荐答案

使用shell = True时,subprocess.Popen的第一个参数应该是字符串,而不是一个列表:

When using shell = True, the first argument to subprocess.Popen should be a string, not a list:

p = subprocess.Popen('gphoto2', shell=True, ...)

但是,如果可能,应避免使用 shell = True,因为它可能是 安全风险(参见警告).

However, using shell = True should be avoided if possible since it can be a security risk (see the Warning).

所以改用

p = subprocess.Popen(['gphoto2'], ...)

(当shell = False,或者省略shell参数时,第一个参数应该是一个列表.)

(When shell = False, or if the shell parameter is omitted, the first argument should be a list.)

这篇关于Python子进程调用返回“command not found",终端正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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