简体   繁体   中英

Python subprocess doesn't recognize commands in $PATH

I'm trying to debug a sublime plugin that stopped working all of the sudden.

I have the following code in the plugin.

proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=info, cwd=home)
data = proc.communicate()[0]

Basically, it's executing a file that has #!/usr/bin/env php at the top. When I run the command, I get env: php: no such file or directory error message.

I fixed it by using absolute path.

Solution 1 : #!/usr/bin/env /Applications/MAMP/bin/php/php5.5.18/bin/php

While this works, I really wonder why it doesn't work when php is available when I execute it in my terminal and the path to the command is defined in $PATH variable.

How can I fix this?

As in the docs you need to pass shell=True , as an argument to 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.

Though the use of shell=True is discourged due to security considerations .

shell关键字参数将有所帮助,但请注意,文档建议在POSIX系统上使用subprocess32模块

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM