Python subprocess call returns quot;command not foundquot;, Terminal executes correctly(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",终端正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python子进程调用返回“command not found",终端正确执行
基础教程推荐
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
