用python代码怎么修改环境变量的值?

怎么调用系统API啊

如果是python内部临时改变就改 os.environ 这个字典就可以了,如果是要永久改设置就需要调系统API了追问

怎么调用系统API啊

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-18
好像是 sys.env,是个字典变量追问

怎么写呢?path后面加一条。

追答

os.environ['path']+=';path1'我在手机上写的,你凑合看

追问

修改了以后仅仅在当前函数有用啊,实际的环境变量没变啊。再开一个程序调用 os.environ['path']还是原来的值

追答

我找到个解答:
To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates. Note that the values of the environment variables listed in this key are limited to 1024 characters.

http://stackoverflow.com/questions/531998/is-there-a-way-to-set-the-environment-path-programatically-in-c-on-windows

相似回答