Python subprocess does not recognize commands in $ PATH

I am trying to debug a sublime plugin that suddenly stops working.

I have the following code in a plugin.

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

      

Basically, it executes the file from the #!/usr/bin/env php

top. When I run the command, I get an error env: php: no such file or directory

.

I fixed it using an absolute path.

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

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

How can I fix this?

+3


source to share


2 answers


As in the docs , you need to pass shell=True

as an argument 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 advanced control flow it offers for most system shells, and still want convenient access to other shell features such as shells, wildcards, environment variable expansion, and extension ~ to users home directory.



Usage is shell=True

rejected for security reasons though .

+2


source


the shell keyword parameter will be helpful, but note that the documentation recommends using subprocess32 on POSIX systems.



+1


source







All Articles