Python子进程调用返回“command not found",终端正确执行

Python subprocess call returns quot;command not foundquot;, Terminal executes correctly(Python子进程调用返回“command not found,终端正确执行)
本文介绍了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",终端正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)