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

我正在尝试调试一个突然停止工作的Sublime插件.我在插件中有以下代码.proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=info, cwd=home)data...

我正在尝试调试一个突然停止工作的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:没有这样的文件或目录错误消息.

我通过使用绝对路径修复了它.

解决方案1:#!/usr/bin/env /Applications/MAMP/bin/php/php5.5.18/bin/php

虽然这有效,但我真的想知道为什么在我在终端中执行php并且在$PATH变量中定义了该命令的路径时,为什么php不可用.

我怎样才能解决这个问题?

解决方法:

与在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中的命令

基础教程推荐