通过批处理或Python永久更改用户的%PATH%环境变量

时间:2022-03-29 23:04:42

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;永久值似乎存储在注册表的某个地方(我不敢踩的地方)。

4 个解决方案

#1


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

我找到了另外两个可以做同样事情的可执行文件。我最喜欢的是Jonathan“Darka”Wilkes在CodeProject上的SetEnv。他使它非常有用,具有良好的功能,并且与所有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_SETTINGCHANGE to top-level windows, or restarting the shell/rebooting). However I think that SetEnv in a BATCH file is your best bet. ;)

另一个选择,如果您愿意,可以手动执行(实际将项目添加到注册表,然后将WM_SETTINGCHANGE广播到*窗口,或重新启动shell /重新启动)。但是我认为BATCH文件中的SetEnv是你最好的选择。 ;)

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

因此,由于我一直难以让我的用户手动设置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 path to 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 path is added to that executable's PATH environment variable.

每当启动给定名称的可执行文件时,特定于应用程序的路径就会添加到该可执行文件的PATH环境变量中。

#3


From this website:

从这个网站:

Using the add-on tool Setx.exe

使用附加工具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

它不是标准Windows XP安装程序的一部分,但Windows XP Service Pack 2支持工具中包含一个名为setx.exe的命令行工具。此工具扩展了set命令,以便可以对环境变量进行永久性更改。例如,要将文件夹C:\ New Folder添加到路径,命令将是

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

(SetEnv会很好,但我不喜欢非*许可证,我总是不想调用子进程......我不介意将SetEnv称为子进程,但根据*,许可证它使用的是非*的,因为它有某种“不做恶”的条款,而且我认为那种法律上含糊不清的限制总是一个滴答作响的定时炸弹。)

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)

这是一个麻省理工学院许可的Python类,可以隐藏直接修改注册表和发送WM_SETTINGCHANGE的工作。 (适合在setup.py中使用)

#1


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

我找到了另外两个可以做同样事情的可执行文件。我最喜欢的是Jonathan“Darka”Wilkes在CodeProject上的SetEnv。他使它非常有用,具有良好的功能,并且与所有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_SETTINGCHANGE to top-level windows, or restarting the shell/rebooting). However I think that SetEnv in a BATCH file is your best bet. ;)

另一个选择,如果您愿意,可以手动执行(实际将项目添加到注册表,然后将WM_SETTINGCHANGE广播到*窗口,或重新启动shell /重新启动)。但是我认为BATCH文件中的SetEnv是你最好的选择。 ;)

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

因此,由于我一直难以让我的用户手动设置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 path to 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 path is added to that executable's PATH environment variable.

每当启动给定名称的可执行文件时,特定于应用程序的路径就会添加到该可执行文件的PATH环境变量中。

#3


From this website:

从这个网站:

Using the add-on tool Setx.exe

使用附加工具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

它不是标准Windows XP安装程序的一部分,但Windows XP Service Pack 2支持工具中包含一个名为setx.exe的命令行工具。此工具扩展了set命令,以便可以对环境变量进行永久性更改。例如,要将文件夹C:\ New Folder添加到路径,命令将是

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

(SetEnv会很好,但我不喜欢非*许可证,我总是不想调用子进程......我不介意将SetEnv称为子进程,但根据*,许可证它使用的是非*的,因为它有某种“不做恶”的条款,而且我认为那种法律上含糊不清的限制总是一个滴答作响的定时炸弹。)

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)

这是一个麻省理工学院许可的Python类,可以隐藏直接修改注册表和发送WM_SETTINGCHANGE的工作。 (适合在setup.py中使用)