windows 通过批处理或 Python 永久更改用户的 %PATH% 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1156723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permanently altering a user's %PATH% environment variable via batch or Python
提问by Xiong Chiamiov
I've been having difficulty with getting my users to set the PATH environment variable manually, I'm looking for a way to do this automatically. A batch file would be preferable, since that would require them to run it themselves (with a warning as to what they're doing), but an addition to the setup.py
is acceptable as well.
我一直难以让我的用户手动设置 PATH 环境变量,我正在寻找一种自动执行此操作的方法。批处理文件会更可取,因为这将要求他们自己运行它(并警告他们正在做什么),但对 的添加setup.py
也是可以接受的。
Other information: SET
only affects the current and derivative shells; the permanent values seem to be stored in the Registry somewhere (a place where I dare not tread).
其他信息:SET
只影响当前和衍生的shell;永久值似乎存储在注册表中的某个地方(我不敢踏足的地方)。
采纳答案by Synetech
As David said, there is the SETX tool that you can get from the Windows Resource Kit.
正如 David 所说,您可以从 Windows Resource Kit 中获得 SETX 工具。
However, I have found that SETX has trouble (like crashing) sometimes. I have not figured out exactly what the problem is, but I suspect it is a size issue (for example if you try to set a variable—in my case it was PATH—to a value that is too big, eg >1024 some odd characters).
但是,我发现 SETX 有时会遇到问题(例如崩溃)。我还没有弄清楚问题到底是什么,但我怀疑这是一个大小问题(例如,如果您尝试将一个变量(在我的情况下是 PATH)设置为一个太大的值,例如 >1024 有点奇怪人物)。
I have found two other executables that can do the same thing. My favorite in particular is SetEnvby Jonathan “Darka” Wilkes over at CodeProject. He has made it quite useful, with good functionality, and it is compatible with all Windows systems—I suggested some features too. :)
我发现了另外两个可以做同样事情的可执行文件。我特别喜欢的是SETENV由乔纳森“Darka”在CodeProject威尔克斯过来。他使它非常有用,具有良好的功能,并且与所有 Windows 系统兼容——我也推荐了一些功能。:)
Another option, if you are up to it, is to do it manually (actually adding the item to the registry and then either broadcasting a WM_SETTINGCHANGEto top-level windows, or restarting the shell/rebooting). However I think that SetEnv in a BATCH file is your best bet. ;)
如果您愿意,另一种选择是手动执行(实际上将该项添加到注册表中,然后将WM_SETTINGCHANGE广播到顶级窗口,或者重新启动外壳程序/重新启动)。但是,我认为 BATCH 文件中的 SetEnv 是您最好的选择。;)
回答by mrsheen
So, since I've been having difficulty with getting my users to set the PATH manually, I'm looking for a way to do this automatically.
因此,由于我一直难以让我的用户手动设置 PATH,因此我正在寻找一种自动执行此操作的方法。
The HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
(as well as HKEY_CURRENT_USER\...
) registry key allows you to attach an application specific pathto your executable name.
的HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
(以及HKEY_CURRENT_USER\...
)注册表项使您可以将附加应用程序特定的路径到你的可执行文件的名称。
Whenever an executable of the given name is started, the application specific pathis added to that executable's PATHenvironment variable.
每当启动给定名称的可执行文件时,应用程序特定路径将添加到该可执行文件的PATH环境变量中。
回答by ssokolow
I just ran across this question and didn't like any of the available options so I decided to write my own solution.
我刚遇到这个问题,不喜欢任何可用的选项,所以我决定编写自己的解决方案。
(SetEnv would've been good, but I didn't like the non-libre license and I always prefer not having to call a subprocess... I wouldn't mind calling SetEnv as a subprocess but, according to Wikipedia, the license it uses is non-libre because it has some kind of "do no evil" clause and that kind of legally-ambiguous restriction is always a ticking time-bomb in my opinion.)
(SetEnv 会很好,但我不喜欢非自由许可证,我总是更喜欢不必调用子进程......我不介意将 SetEnv 作为子进程调用,但是,根据维基百科,许可证它使用的是非自由的,因为它有某种“不作恶”的条款,而在我看来,这种法律上模棱两可的限制总是一个定时炸弹。)
Here's a little MIT-licensed Python classto hide away the work of modifying the registry directly and sending the WM_SETTINGCHANGE. (Good for use in setup.py
)
这是一个小的MIT 许可的 Python 类,用于隐藏直接修改注册表和发送 WM_SETTINGCHANGE 的工作。(适用于setup.py
)
回答by David Archer
From this website:
从这个网站:
Using the add-on tool Setx.exe
It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools. This tool extends the set command so that permanent changes in the environment variables can be made. For example, to add a folder C:\New Folder to the path, the command would be
setx path "%PATH%;C:\New Folder"
使用附加工具 Setx.exe
它不是标准 Windows XP 安装程序的一部分,但名为 setx.exe 的命令行工具包含在 Windows XP Service Pack 2 支持工具中。此工具扩展了 set 命令,以便可以对环境变量进行永久更改。例如,要将文件夹 C:\New Folder 添加到路径中,命令将是
setx path "%PATH%;C:\New Folder"
This sounds like it'll work for what you're wanting to do.
这听起来像你想做的事情一样。