Passing environment variable to "subprocess" causes failure

D

davidgould

I'm attempting to run subprocess and passing in an environment
variable. When I do this the child process fails with an error. When I
don't pass an environement variable it runs fine.

BTW Running this code under Windows XP with Python 2.6.1

Code A:

p = subprocess.Popen( ['python', '-V'], env={ 'PYTHONPATH': 'C:/
Documents and Settings/David Gould/workspace/DgTools/Common/Trunk/
Source' } )
print p.communicate()[0]
print p.returncode

Output:

None
-1072365564

Code B:

p = subprocess.Popen( ['python', '-V'] )
print p.communicate()[0]
print p.returncode

Output:

Python 2.6.1
0

Any idea why passing an environment variable causes it to fail?
Thanks.
 
M

Miki

Code A:

p = subprocess.Popen( ['python', '-V'], env={ 'PYTHONPATH': 'C:/
Documents and Settings/David Gould/workspace/DgTools/Common/Trunk/
Source' } )
print p.communicate()[0]
print p.returncode

Output:

None
-1072365564
My *guess* is that since PATH is not in the environment you're
passing, the shell can't find the python interpreter.
Try:
from os import environ
from subprocess import Popen, PIPE
env = environ.copy()
# Warning: override existing PYTHONPATH
env["PYTHONPATH"] = "C:/Documents and Settings/David Gould/workspace/
DgTools/Common/Trunk/Source"
p = = subprocess.Popen(['python', '-V'], env=env, stdout=PIPE)
print p.communicate()
print p.returncode

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top