擅长:python、mysql、java
<p>使用外部Windows <code>setx</code>命令可能同样简单:</p>
<pre><code>C:\>set NEWVAR
Environment variable NEWVAR not defined
C:\>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('setx NEWVAR newvalue')
0
>>> os.getenv('NEWVAR')
>>> ^Z
C:\>set NEWVAR
Environment variable NEWVAR not defined
</code></pre>
<p>现在打开一个新的命令提示符:</p>
<pre><code>C:\>set NEWVAR
NEWVAR=newvalue
</code></pre>
<p>如您所见,<code>setx</code>既不为当前会话设置变量,也不为父进程(第一个命令提示符)设置变量。但它确实在注册表中为将来的进程持久地设置了变量。</p>
<p>我认为根本没有办法改变父进程的环境(如果有,我很乐意听到)。</p>