我对 Windows 上 pip
的 --user
选项感到困惑。似乎在 Windows 上,包将始终安装到我的主目录 (C:\Users\{username}\...
) 而不管 --user
是否存在,而在 Linux 上,如果省略 --user
,则软件包将安装到全局位置:
C:\>pip3 install pyyaml
Collecting pyyaml
Using cached https://files.pythonhosted.org/packages/45/19/53c041b8719eaf88ce1cdb51bea1c5a2844433e79c23a2a8aeeaa0e27fd8/PyYAML-5.1.1-cp37-cp37m-win32.whl
Installing collected packages: pyyaml
Successfully installed pyyaml-5.1.1
C:\>pip3 show pyyaml
Name: PyYAML
Version: 5.1.1
Summary: YAML parser and emitter for Python
Home-page: https://github.com/yaml/pyyaml
Author: Kirill Simonov
Author-email: xi@resolvent.net
License: MIT
Location: c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages
Requires:
Required-by:
C:\>pip3 uninstall pyyaml
Uninstalling PyYAML-5.1.1:
Would remove:
c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\_yaml.cp37-win32.pyd
c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\pyyaml-5.1.1.dist-info\*
c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\yaml\*
Proceed (y/n)? y
Successfully uninstalled PyYAML-5.1.1
C:\>pip3 install --user pyyaml
Collecting pyyaml
Using cached https://files.pythonhosted.org/packages/45/19/53c041b8719eaf88ce1cdb51bea1c5a2844433e79c23a2a8aeeaa0e27fd8/PyYAML-5.1.1-cp37-cp37m-win32.whl
Installing collected packages: pyyaml
Successfully installed pyyaml-5.1.1
C:\>pip3 show pyyaml
Name: PyYAML
Version: 5.1.1
Summary: YAML parser and emitter for Python
Home-page: https://github.com/yaml/pyyaml
Author: Kirill Simonov
Author-email: xi@resolvent.net
License: MIT
Location: c:\users\{username}\appdata\roaming\python\python37\site-packages
Requires:
Required-by:
以上所有命令都是在普通用户下运行的,不是管理员。
那么pip的--user
选项在Windows上有什么用呢?每次安装包时是否都需要使用 --user
选项?
最佳答案
Windows Python 安装程序如何使用默认设置(您很可能使用过)处理安装位置确实令人困惑。
根据 the documentation ,当您运行安装程序并单击“立即安装”时:
- You will not need to be an administrator (unless a system update for the C Runtime Library is required or you install the Python Launcher for Windows for all users)
- Python will be installed into your user directory
- The Python Launcher for Windows will be installed according to the option at the bottom of the first page.
现在,Python 启动器的选项也默认选中。这意味着,如果您的用户帐户位于“管理员”组(通常是这样),则 Python (python.exe
) 将安装在您的 %LocalAppData%
目录中(正如您所观察到的)。但是,安装程序仍会向您显示 UAC 提示以确认您具有管理员权限,因为它还想安装 Python 启动器 (py.exe
)。你最终在你的用户目录中安装了一个“只为我”的本地 Python,尽管出于一些无形的原因,Launcher 是“为所有用户”安装的。
如果你真的想在系统范围内安装 Python,你需要在安装程序的第一个屏幕上选择“自定义安装”,然后单击“下一步”,并勾选“为所有用户安装”——这不是否则检查。安装位置将默认为您的 %ProgramFiles%
目录,通常是 C:\Program Files
。
当您使用 pip
进行用户安装时,它会将软件包放入您的 %AppData%
目录,即 AppData\Roaming
在您的用户个人资料中(正如您也观察到的那样)。这样一来,当您在域网络上拥有“漫游”帐户时,无论您从网络上的哪台计算机登录,您个人安装的软件包都会跟随您。显然,该计算机必须具有系统范围的 Python “为所有用户”安装——Python 解释器,而不是几乎不相关的 Python Launcher。这就是上面描述的默认行为绝对零意义的地方,因为当您登录到他们的计算机。
最重要的是,如果您确实确实使用您的配置文件“漫游”域网络,那么所有那些--user
包,这很可能,包含数千个文件,将减慢登录过程:这些文件中的每一个都必须在域存储和本地计算机之间同步。
底线:如果您想正确设置它,请自定义您的安装以确保它安装在事实上所有用户都可以访问的目录中。就我个人而言,我喜欢将它放在 C:\programs\Python
中,这样我就可以为每个人pip install
任何东西,甚至不需要提升的提示 - which one将用于写入 C:\Program Files
。话又说回来,根据具体情况,要求提升提示可能是可取的。
关于python - Windows 上 pip 的 --user 选项有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56751483/