打开命令行时Windows环境变量发生变化?
问题描述
有时当我在 Windows 中更改我的环境变量,然后使用依赖于这些变量的软件时,它们没有正确更新.
Sometimes when I change my environment variables in Windows, and then use software the depends on those variables, they are not properly updated.
一个很好的例子是更改一个变量,然后打开 Windows 命令行并回显该变量并查看它没有被更改,即使您在环境变量"窗口中正确更改了它.
And good example is to change a variable, then open up Windows Command Line and echo the variable and see that it hasn't been changed, even though you properly changed it in the Environment Variables window.
我现在正在处理的另一个例子:
Another example I'm dealing with right now:
我在一个项目中使用 Python 2.4.x 已经有一段时间了,它使用了 env var PYTHONPATH
的值是:
I've been using Python 2.4.x for a while for a project, which uses the env var PYTHONPATH
who's value has been:
C:Python24;C:Python24lib
今天我为该项目安装了 Python 2.5.x.我将 PYTHONPATH 更改为:
Today I installed Python 2.5.x for the project. I changed my PYTHONPATH to be:
C:Python25;C:Python25lib
当我使用 Python 2.5 运行脚本并执行此操作时:
When I use Python 2.5 to run a script and do this:
import sys
print sys.path
打印出来:
'C:\PYTHON24', 'C:\PYTHON24\lib' (and some other Python 2.5 related default installation paths)
很明显,旧的 PYTHONPATH
环境变量更改并没有真正坚持......
So clearly, the old PYTHONPATH
environment variable changes aren't really sticking....
有谁知道为什么会发生这种情况以及如何解决它?
Does anyone know why this happens and how to fix it?
解决方案
当您在系统属性"选项卡中更改环境变量时,新值将传播到 Windows 资源管理器,并打开任何应用程序(如 cmd.exe)从 Windows 资源管理器(或运行框、开始菜单等)应该会看到新值.
When you change an environment variable in the System Properties tab, the new value will propagate to the Windows Explorer, and any apps (such as cmd.exe) opened from the Windows Explorer (or the Run box, Start Menu, etc.) should see the new value.
但是,如果您正在运行诸如编辑器或 python 或某些非 Microsoft 程序启动器之类的程序,则更改环境变量,然后从该程序(而不是 Windows 资源管理器)启动 cmd.exe,您很可能查看环境变量的旧值.原因是正在运行的程序忽略了来自Windows的通知,说环境发生了变化(一点也不异常),并且由于启动的进程继承了环境变量,所以子进程不会看到变化.
However, if you're running a program such as an editor or python or some non-Microsoft program launcher, then change an environment variable, and then launch cmd.exe from that program (instead of Windows Explorer) you are likely to see the old value of the environment variable. The reason is that the running program ignored the notification from Windows saying that the environment has changed (not at all unusual), and since the launched process inherits the environment variables, the child process won't see the changes.
解决方法是确保从 Windows 资源管理器或运行"框启动应用.重新启动您的机器也可以工作(如果重新启动不能解决问题,那么还有其他问题).
The workaround is to make sure you start your app from Windows Explorer or the Run box. Rebooting your machine will work also (if rebooting doesn't solve the problem, then something else is going on).
相关文章