Python:OSError:[Errno 2]subprocess.Popen上没有这样的文件或目录

2024-06-01 21:51:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在.bashrc中将路径添加到mytool,并且可以从bash shell中的任何路径运行mytool --help。但是,当我运行以下代码片段时,会得到:

File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory

import subprocess

command_array = ['mytool', '--help']

p = subprocess.Popen(command_array,
                     stderr=subprocess.STDOUT, stdout=subprocess.PIPE,
                    )

for line in iter(p.stdout.readline, b''):
        print(line)
p.stdout.close()

我该怎么解决?

编辑:当我从终端(bash)运行python文件时,它工作得很好。但是当我从PyCharm(调试器)或其他shell运行时,它给出了上述错误。

当我从其他shell运行脚本时,如何更改脚本使其在bash中运行“mytool”?我需要在.bashrc中添加环境


Tags: in路径脚本bashchildstdoutlinehelp
2条回答

将此打印添加到您的文件:

import os
print os.environ['PATH']

从IDE和终端运行脚本后比较输出。
您应该注意到IDE的PATH不包括mytool的目录。

转到:

Run/Debug Configurations->;Environment variables

添加休闲:

PATH=输出到shell echo $PATH

相关问题 更多 >