I am having an issue with setting an environment variable on a call to subprocess.Popen
. The environment variable does not seem to be getting set. Any suggestions on how to properly set environmental variables for a Python commandline call?
My goal is to run a script that uses an environmental variable determined from my Python code:
d = dict(os.environ)
d["TEST_VARIABLE"] = str(1234)
subprocess.Popen('/usr/bin/mybinary', env=d).wait()
but the script reacts as if the variable has never been set
Here is my attempt to test, using Python's interactive interpreter:
d = dict(os.environ)
d["TEST_VARIABLE"] = str(1234)
subprocess.Popen(['/bin/echo', '$TEST_VARIABLE'], env=d).wait()
and the output is:
"$TEST_VARIABLE"
0
I thought env=d
should set the environment for the subprocess, but it apparently does not. Any suggestions on how to correct this issue?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…