打开命令行时Windows环境变量会发生变化吗? [英] Windows environment variables change when opening command line?

查看:34
本文介绍了打开命令行时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:\Python24\lib

今天我为项目安装了 Python 2.5.x.我将 PYTHONPATH 更改为:

Today I installed Python 2.5.x for the project. I changed my PYTHONPATH to be:

C:\Python25;C:\Python25\lib

当我使用 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).

这篇关于打开命令行时Windows环境变量会发生变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆