CMD和PowerShell等进程在启动时会获取环境的副本。修改原始环境变量时,此副本不会更新。除了修改系统设置中的变量外,还需要重新启动进程以获取更新的值或修改复制的变量。
示范:
PS C:\>echo $env:FOO
PS C:\>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import os
>>>print(os.getenv('FOO'))
None
>>>exit()
PS C:\>$env:FOO = 'bar'
PS C:\>echo $env:FOO
bar
PS C:\>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import os
>>>print(os.getenv('FOO'))
bar
如果变量通过GUI环境变量编辑器设置,然后资源管理器的环境应该已经被更新。您不应该重新启动系统。但是,您必须从资源管理器重新启动Python(或cmd/PowerShell,然后是Python)以查看更新后的环境。 – eryksun
我明白了,我可能只是需要重新启动我的外壳。感谢您的澄清! – Cdhippen