wim wim - 1 year ago 369
Python Question

python subprocess Popen environment PATH?

I'm confused about how

searches for the executable when using
. It works if given absolute paths to the child process, but I'm trying to use relative paths. I've found that if I set the environment variable PYTHONPATH then I can get imported modules from that path ok, and PYTHONPATH is there in
, but it doesn't seem to help with the behaviour of
. I've also tried editing the
file adding PYTHONPATH to
, like so

# copy PYTHONPATH environment variable into PATH to allow our stuff to use
# relative paths for subprocess spawning
import os
if os.getenv('PYTHONPATH') is not None and os.getenv('PATH') is not none:
os.environ['PATH'] = ':'.join([os.getenv('PATH'), os.getenv('PYTHONPATH')])

and verified that when starting up python , either interactively, with ipython, or by running a script from the command line, that PYTHONPATH is successfully appearing in
. However,
still doesn't search there for the executable. I thought it was supposed to inherit the parents environment, if no
kwarg is specified? Next I tried giving
explicitly, first by making a copy of
and secondly just by giving
env={'PATH': '/explicit/path/to/search/from'}
, and it still does not find the executable. Now I'm stumped.

Hopefully an example will help explain my problem more clearly:



# some_script.py
from subprocess import Popen, PIPE
spam, eggs = Popen(['../subdir1/some_executable'], stdout=PIPE, stderr=PIPE).communicate()

If I'm in
and I run
python some_script.py
it works, but if I'm in
and I run
python subdir2/some_script.py
even though
is in the
, then subprocess will throw
OSError: [Errno 2] No such file or directory

Answer Source

(filling in details from a comment to make a separate answer)

First off, relative paths (paths containing slashes) never get checked in any PATH, no matter what you do. They are relative to the current working directory only. If you need to resolve relative paths, you will have to search the PATH manually, or munge the PATH to include the subdirectories and then just use the command name as in my suggestion below.

If you want to run a program relative to the location of the Python script, use __file__ and go from there to find the absolute path of the program, and then use the absolute path in Popen.

Secondly, there is an issue in the Python bug tracker about how Python deals with bare commands (no slashes). Basically, on Unix/Mac Popen uses os.execvp when invoked with shell=False, which means it looks at the value of PATH as it was when Python launched and no amount of changing os.environ will help you fix that. Also, on Windows with shell=False, it pays no attention to PATH at all, and will only look in relative to the current working directory.

If you JUST need path evaluation and don't really want to run your command line through a shell, and are on UNIX, I advise using env instead of shell=True, as in Popen(['/usr/bin/env', 'progtorun', other, args], ...). This lets you pass a different PATH to the env process, which will use it to find the program. It also avoids issues with shell metacharacters and potential security issues with passing arguments through the shell. Obviously, on Windows (pretty much the only platform without a /usr/bin/env) you will need to do something different.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download