php - Python 子进程无法识别 $PATH 中的命令

标签 php python shell

我正在尝试调试一个突然停止工作的 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/

相关文章:

php - 查询未更新,不确定原因

PHP选择具有相同id的不同列

PHP:如果MYSQL结果大于0,添加到数组吗?

javascript - Jquery 在同一脚本中加载

bash - UNIX/shell 脚本中的字符串连接

python - Babel numbers format_currency 函数

基于公共(public)键合并 2 个字典列表的 Pythonic 方法

python - 导入整个 .txt 的最佳方法。文件在单个 pandas 行中

r - 如何通过 shell 从父目录获取 R 文件?

bash - shell脚本中的非法数字