Permanently altering a user's %PATH% environment variable via batch or Python

时间秒杀一切 提交于 2020-01-12 05:23:06

问题


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.

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


回答1:


As David said, there is the SETX tool that you can get from the Windows Resource Kit.

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

I have found two other executables that can do the same thing. My favorite in particular is SetEnv by 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. :)

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_SETTINGCHANGE to top-level windows, or restarting the shell/rebooting). However I think that SetEnv in a BATCH file is your best bet. ;)




回答2:


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.

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 path to your executable name.

Whenever an executable of the given name is started, the application specific path is added to that executable's PATH environment variable.




回答3:


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"

This sounds like it'll work for what you're wanting to do.




回答4:


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

Here's a little MIT-licensed Python class to hide away the work of modifying the registry directly and sending the WM_SETTINGCHANGE. (Good for use in setup.py)



来源:https://stackoverflow.com/questions/1156723/permanently-altering-a-users-path-environment-variable-via-batch-or-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!