Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - Windows environment variables change when opening command line?

Sometimes when I change my environment variables in Windows, and then use software the depends on those variables, they are not properly updated.

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:

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

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

C:Python25;C:Python25lib

When I use Python 2.5 to run a script and do this:

import sys
print sys.path

It prints:

'C:\PYTHON24', 'C:\PYTHON24\lib' (and some other Python 2.5 related default installation paths)

So clearly, the old PYTHONPATH environment variable changes aren't really sticking....

Does anyone know why this happens and how to fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...