执行shell脚本时出现Python错误

时间:2021-07-06 15:29:12

I'm executing a line of shell script using python. It isn't working with which to test of the existence of homebrew

我正在使用python执行一行shell脚本。它无法测试自制软件的存在

#!/usr/bin/python
import sys, subprocess
subprocess.call(["which python"])

throws the long error

抛出长错误

Traceback (most recent call last):
  File "installations.py", line 5, in <module>
    subprocess.call(["which python"]) 
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

But I know that the shell execution is working correctly on some level

但我知道shell执行在某种程度上正常工作

#!/usr/bin/python
import sys, subprocess
subprocess.call(["whoami]) 

prints my username just fine. Am I doing something wrong, or is which for some reason not supported. Is there some better supported way of detecting the existence of an installation?

打印我的用户名就好了。我做错了什么,或者出于某种原因不支持。是否有一些更好的支持方式来检测安装的存在?

2 个解决方案

#1


The failing call is trying to find an executable named 'which python' not running which with an argument of python as you likely intended. The list that you pass to call (unless shell=True is set) is the list of the command and all the arguments. Doing

失败的调用试图找到一个名为'which python'的可执行文件,它正在运行,你可能想要使用python的参数。传递给调用的列表(除非设置了shell = True)是命令列表和所有参数。干

subprocess.call(['which', 'python'])

will probably give you what you are looking for.

可能会给你你想要的东西。

#2


When calling this way you need to separate each word on your command line as a different element of an iterable:

当以这种方式调用时,您需要将命令行上的每个单词分隔为iterable的不同元素:

subprocess.call(["which", "python"])

I highly recommend reading the docs several times through in order to truly understand all the possible ways of launching subprocesses and how they differ from each other.

我强烈建议多次阅读文档,以便真正了解启动子流程的所有可能方式以及它们之间的差异。

#1


The failing call is trying to find an executable named 'which python' not running which with an argument of python as you likely intended. The list that you pass to call (unless shell=True is set) is the list of the command and all the arguments. Doing

失败的调用试图找到一个名为'which python'的可执行文件,它正在运行,你可能想要使用python的参数。传递给调用的列表(除非设置了shell = True)是命令列表和所有参数。干

subprocess.call(['which', 'python'])

will probably give you what you are looking for.

可能会给你你想要的东西。

#2


When calling this way you need to separate each word on your command line as a different element of an iterable:

当以这种方式调用时,您需要将命令行上的每个单词分隔为iterable的不同元素:

subprocess.call(["which", "python"])

I highly recommend reading the docs several times through in order to truly understand all the possible ways of launching subprocesses and how they differ from each other.

我强烈建议多次阅读文档,以便真正了解启动子流程的所有可能方式以及它们之间的差异。