I'm confused about how
# copy PYTHONPATH environment variable into PATH to allow our stuff to use
# relative paths for subprocess spawning
if os.getenv('PYTHONPATH') is not None and os.getenv('PATH') is not none:
os.environ['PATH'] = ':'.join([os.getenv('PATH'), os.getenv('PYTHONPATH')])
from subprocess import Popen, PIPE
spam, eggs = Popen(['../subdir1/some_executable'], stdout=PIPE, stderr=PIPE).communicate()
OSError: [Errno 2] No such file or directory
(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
Secondly, there is an issue in the Python bug tracker about how Python deals with bare commands (no slashes). Basically, on Unix/Mac
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.