我正在尝试调试一个突然停止工作的 sublime 插件。
我在插件中有以下代码。
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=info, cwd=home)
data = proc.communicate()[0]
基本上,它正在执行一个顶部有 #!/usr/bin/env php
的文件。当我运行命令时,我收到 env: php: no such file or directory
错误消息。
我用绝对路径修复了它。
解决方案一:#!/usr/bin/env/Applications/MAMP/bin/php/php5.5.18/bin/php
虽然这行得通,但我真的想知道为什么当我在终端中执行 php 并且命令的路径在 $PATH 变量中定义时它不起作用。
我该如何解决这个问题?
最佳答案
如 docs您需要将 shell=True
作为参数传递给 subprocess.Popen
:
If shell is True, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user’s home directory.
尽管不鼓励使用 shell=True
due to security considerations .
关于php - Python 子进程无法识别 $PATH 中的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30632537/