Re: Passing environment variable to subprocess causes failure
davidgo...@davidgould.com wrote: 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. Are you sure it's not the path within the dict you're passing that's wrong? -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing environment variable to subprocess causes failure
Quoth MRAB goo...@mrabarnett.plus.com: davidgo...@davidgould.com wrote: 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. Are you sure it's not the path within the dict you're passing that's wrong? Or there's something missing from the environment that python needs, causing a non-zero return code when it is missing. (That seems odd, though.) You ought to print communiicate()[1] to see if there is an accompanying error message that would shed light on the problem. --RDM -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing environment variable to subprocess causes failure
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, -- Miki miki.teb...@gmail.com http://pythonwise.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list