在 Anaconda 下在 Windows 上设置 PYTHONPATH,无需提升权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43099096/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:30:43  来源:igfitidea点击:

Setting PYTHONPATH on Windows under Anaconda without elevated privileges

pythonwindowsenvironment-variablesanacondapythonpath

提问by Mad Physicist

I have a Windows 7 machine with a 32-bit install of anaconda, installed for the local user. The install works as expected: it allows me to run python and pip from the command line, switch environments, etc.

我有一台装有 32 位 anaconda 的 Windows 7 机器,是为本地用户安装的。安装按预期工作:它允许我从命令行运行 python 和 pip、切换环境等。

I also have a library of code that I wrote sitting in My Documents. I would like to add an entry like C:\Users\username\Documents\MyLibto my PYTHONPATH. This would normally not be an issue, but I do not have the elevated privileges that this (work-issued) computer requires to modify environment variables through the Windows UI.

我还有一个我在我的文档中编写的代码库。我想C:\Users\username\Documents\MyLib在我的PYTHONPATH. 这通常不是问题,但我没有这台(工作发布的)计算机通过 Windows UI 修改环境变量所需的提升权限。

I am looking for a way to set PYTHONPATH. I only need it to work within the anaconda environment (i.e., I just want to be able to import my library using that particular interpreter). Solutions using sys.path.appendwithin my scripts are not acceptable. I am, however, OK with doing sys.path.appendin some script that will be executed automatically whenever I run python myscript.py.

我正在寻找一种设置 PYTHONPATH 的方法。我只需要它在 anaconda 环境中工作(即,我只想能够使用该特定解释器导入我的库)。sys.path.append在我的脚本中使用的解决方案是不可接受的。但是,我可以sys.path.append在某些脚本中执行该脚本,该脚本将在我运行时自动执行python myscript.py

I suspect that there is a way to get around the privilege restrictions imposed through the Windows UI though, because running conda info -ashows that the Anaconda installer was able to insert items into the PATH, specifically, C:\Users\username\AppData\Local\Continuum\Anaconda3\Library\bin;....

我怀疑是有办法让周围通过Windows UI,虽然规定的权限限制,因为运行conda info -a表明Anaconda安装能够将项目插入的PATH,具体而言,C:\Users\username\AppData\Local\Continuum\Anaconda3\Library\bin;...

回答by ImportanceOfBeingErnest

Since you said, you mostly use Spyder, the following might be useful:

既然你说,你主要使用 Spyder,以下可能有用:

Spyder offers the option to manage PYTHONPATH through an integrated manager.

Spyder 提供了通过集成管理器管理 PYTHONPATH 的选项。

enter image description here

在此处输入图片说明

回答by Eryk Sun

In the Control Panel window for modifying user accounts, there should be an option to change the environment variables just for the current user. It's broken on some versions of Windows, but if it works it's the simplest option.

在用于修改用户帐户的控制面板窗口中,应该有一个选项可以仅为当前用户更改环境变量。它在某些版本的 Windows 上坏了,但如果它有效,它是最简单的选择。

If that doesn't work, the next simplest option is to use setx.exe in a command prompt. It defaults to the current user. For example:

如果这不起作用,下一个最简单的选择是在命令提示符中使用 setx.exe。它默认为当前用户。例如:

setx.exe PYTHONPATH "C:\Users\username\Documents\MyLib"

You can also use reg.exe to set the variable manually in the registry. But unlike the above options, this doesn't broadcast a WM_SETTINGCHANGEmessage to top-level windows. When Explorer sees that message it reloads its environment from the registry. Without it, you'll have to log off and back on again to see the updated environment variable. With that said, here's an example command using reg.exe:

您还可以使用 reg.exe 在注册表中手动设置变量。但与上述选项不同的是,这不会WM_SETTINGCHANGE向顶级窗口广播消息。当 Explorer 看到该消息时,它会从注册表重新加载其环境。没有它,您必须注销并重新登录才能查看更新的环境变量。话虽如此,这是一个使用 reg.exe 的示例命令:

reg.exe add HKCU\Environment /f /v PYTHONPATH /d "C:\Users\username\Documents\MyLib"


I'm not a fan of permanently setting PYTHONPATHbecause the same variable gets used by every version of Python. I'd rather create a shortcut to a batch script that configures the environment the way I need it for a specific task. For example:

我不喜欢永久设置,PYTHONPATH因为每个版本的 Python 都使用相同的变量。我宁愿创建一个批处理脚本的快捷方式,该脚本按照特定任务所需的方式配置环境。例如:

@echo off
set PYTHONPATH=C:\Users\username\Documents\MyLib
C:\Users\username\Documents\MyEnv\Scripts\activate.bat

Create a shortcut to this batch script. Then right-click the shortcut and select "Properties". Modify the target to run cmd.exe /k "path\to\the\script.bat".

创建此批处理脚本的快捷方式。然后右键单击快捷方式并选择“属性”。修改目标运行cmd.exe /k "path\to\the\script.bat"