微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐