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

问题描述

我正在尝试从 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子进程调用返回“未找到命令",终端正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:15