Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess doesn't recognize commands in $PATH

Tags:

python

shell

php

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?

like image 766
Moon Avatar asked Jul 30 '26 14:07

Moon


1 Answers

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.

like image 60
behzad.nouri Avatar answered Aug 02 '26 05:08

behzad.nouri