<div><a href="http://code.activestate.com/recipes/159462-how-to-set-environment-variables/">http://code.activestate.com/recipes/159462-how-to-set-environment-variables/</a></div><div><br></div><div>Writes environment variables using a batch file wrapper. Overcomes an operating system limitation.</div>
<div><br></div><div><i>setvar.bat</i></div><div><i>----------</i></div><div><i>@echo off</i></div><div><i>python setvarp.py %1 %2 %3 %4 %5 %6 %7 %8 %9</i></div><div><i>settmp</i></div><div><i>del settmp.bat</i></div><div>
<i><br></i></div><div><i>setvarp.py</i></div><div><i>----------</i></div><div><i>import sys, time, math</i></div><div><i>key = sys.argv[1]</i></div><div><i>value = eval(' '.join(sys.argv[2:]))</i></div><div><i>command = 'set %s=%s\n' % (key, value)</i></div>
<div><i>open('settmp.bat', 'w').write(command)</i></div><div><i><br></i></div><div><i><br></i></div><div><i>sample command line session</i></div><div><i>---------------------------</i></div><div><i>C>setvar ts time.ctime()</i></div>
<div><i>C>setvar pi 22.0 / 7.0</i></div><div><i>C>setvar pyver sys.version</i></div><div><i>C>set</i></div><div><i><br></i></div><div><i>TS=Sun Oct 27 18:12:23 2002</i></div><div><i>PI=3.14285714286</i></div><div>
<i>PYVER=2.3a0 (#29, Oct 22 2002, 01:41:41) [MSC 32 bit (Intel)]</i></div><div><br></div><div>Environment variables can be read with os.environ. They can be written (for sub-shells only) using os.putenv(key, value). However, there is no direct way to modify the global environment that the python script is running in. The indirect method shown above writes a set command to a temporary batch file which is in the enclosing environment by another batch file used to launch the python script.</div>
<div><br></div><div>In the example above, arbitrary expressions can be evaluated and the result assigned to an environment variable. For security, the eval() function can be replaced with str().</div><div><br></div><div>
Usually, writing to an environment variable should be avoided in favor of sharing values through a pipe or a common data file. However, when it can't be avoided, the above technique is an effective, though hackish, work-around.</div>
<div><br></div><div><br></div><div>from page <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen">http://docs.python.org/library/subprocess.html#subprocess.Popen</a>,we know the constructor for Popen is</div>
<div>class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None,close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)?</div>
<div>we know after calling Popen, it's won't affect the parent environment, but, with many conditions, we wan't to modify the current environment variables by outer Bash script or DOS bash script. So I suppose to add an extra parameter update_parent_env (Default to False), to implement such a function.</div>
<div><br></div><div>-- </div> 此致<br>礼<br>罗勇刚<br>Yours<br> sincerely,<br>Yonggang Luo<br><br>