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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31362113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:19:51  来源:igfitidea点击:

Python: OSError: [Errno 2] No such file or directory on subprocess.Popen

pythonlinuxbashpycharmpopen

提问by Nemo

I have added the path to mytool in .bashrc and I could run mytool --helpfrom any path in bash shell. However when when I run the following snippet, I get :

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

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

文件“/usr/lib/python2.7/subprocess.py”,第 1249 行,在 _execute_child raise child_exception OSError: [Errno 2] 没有那个文件或目录

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()

How can I resolve this?

我该如何解决这个问题?

EDIT: When I run the python file from terminal (bash), it works fine. But when I run from PyCharm (debugger) or other shells it is giving the above error.

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

How do I change my script so that it runs 'mytool' in bash when I run the script from other shells? I need the environment added in .bashrc

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

回答by Eugeniu Rosca

Add this print to your file:

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

import os
print os.environ['PATH']

Compare the outputs after running the script from IDE and terminal.
You should observe that the IDE's PATHdoes not include the mytool's directory.

从 IDE 和终端运行脚本后比较输出。
您应该观察到 IDEPATH不包含mytool's 目录。

回答by Nir Vana

Go to:

去:

Run/Debug Configurations-> Environment variables

Run/Debug Configurations-> Environment variables

Add fallowing:

添加休耕:

PATH= to output to you shell echo $PATH

PATH= 输出到你的 shell echo $PATH