问题:“ pip install –user…”的目的是什么?

来自pip install --help

 --user      Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on
             Windows. (See the Python documentation for site.USER_BASE for full details.)

site.USER_BASE的文档是一个令人困扰的有趣的* NIX主题,我不明白。

为什么安装软件包很~/.local/重要?为什么不将可执行文件放在$ PATH中?

From pip install --help:

 --user      Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on
             Windows. (See the Python documentation for site.USER_BASE for full details.)

The documentation for site.USER_BASE is a terrifying wormhole of interesting *NIX subject matter that I don’t understand.

Why would intalling the package to ~/.local/ matter? Why not just put an executable somewhere in my $PATH?


回答 0

pip默认将Python软件包安装到系统目录(例如/usr/local/lib/python3.4)。这需要root访问。

--user 而是在您的主目录中进行pip安装软件包,该软件包不需要任何特殊特权。

pip defaults to installing Python packages to a system directory (such as /usr/local/lib/python3.4). This requires root access.

--user makes pip install packages in your home directory instead, which doesn’t require any special privileges.


回答 1

--user安装在中site.USER_SITE

就我而言,是/Users/.../Library/Python/2.7/bin。因此,我已将其添加到我的PATH(~/.bash_profile文件中):

export PATH=$PATH:/Users/.../Library/Python/2.7/bin

--user installs in site.USER_SITE.

For my case, it was /Users/.../Library/Python/2.7/bin. So I have added that to my PATH (in ~/.bash_profile file):

export PATH=$PATH:/Users/.../Library/Python/2.7/bin

回答 2

其他答案提到site.USER_SITE放置Python包的位置。如果您要查找二进制文件,请输入{site.USER_BASE}/bin

如果要将此目录添加到外壳程序的搜索路径中,请使用:

export PATH="${PATH}:$(python3 -c 'import site; print(site.USER_BASE)')/bin"

Other answers mention site.USER_SITE as where Python packages get placed. If you’re looking for binaries, these go in {site.USER_BASE}/bin.

If you want to add this directory to your shell’s search path, use:

export PATH="${PATH}:$(python3 -c 'import site; print(site.USER_BASE)')/bin"

回答 3

只是警告:

根据此问题--user当前在虚拟环境中无效pip,因为用户位置对于虚拟环境而言实际上没有任何意义。

因此,请勿pip install --user some_pkg 在虚拟环境内使用,否则,虚拟环境pip会被混淆。有关更多详细信息,请参见此答案

Just a warning:

According to this issue, --user is currently not valid inside a virtual env’s pip, since a user location doesn’t really make sense for a virtual environment.

So do not use pip install --user some_pkg inside a virtual environment, otherwise, virtual environment’s pip will be confused. See this answer for more details.


回答 4

最好的方法是安装virtualenv,不需要--user混乱。您将获得更大的灵活性,而不必担心每次pip安装软件包时都会破坏不同的python版本和项目。

https://virtualenv.pypa.io/en/stable/

Best way to is install virtualenv and not require the --user confusion. You will get more flexibility and not worry about clobbering the different python versions and projects everytime you pip install a package.

https://virtualenv.pypa.io/en/stable/


回答 5

在macOS上,使用该--user标志的原因是确保我们不会破坏OS依赖的库。一个保守许多的MacOS用户的做法是为了避免在安装或使用,需要一个命令更新点子sudo。因此,这包括安装到/usr/local/bin

参考:为Neovim安装python(https://github.com/zchee/deoplete-jedi/wiki/Setting-up-Python-for-Neovim

我不是所有清楚为什么安装到/usr/local/bin是鉴于该系统只依赖于Python的二进制文件在Mac上的危险/Library/Frameworks//usr/bin。我怀疑是因为如上所述,安装到/usr/local/binrequire sudo可以打开导致系统库代价高昂的错误的大门。因此,安装到内部~/.local/bin是避免这种风险的可靠方法。

参考:在Mac上使用python(https://docs.python.org/2/using/mac.html

最后,程度有安装软件包到的好处/usr/local/bin,我不知道它是有道理的,从改变目录的所有者rootuser这样可以避免必须使用,sudo同时仍可以防止进行依赖于系统的更改。*这是否是安全性默认设置,它是过去更频繁地使用Unix系统(作为服务器)的结果吗?或者至少,对于不托管服务器的Mac用户,这是一个好方法吗?

*注意:Mac的系统完整性保护(SIP)功能似乎还可以保护用户免于更改依赖于系统的库。

-E

On macOS, the reason for using the --user flag is to make sure we don’t corrupt the libraries the OS relies on. A conservative approach for many macOS users is to avoid installing or updating pip with a command that requires sudo. Thus, this includes installing to /usr/local/bin

Ref: Installing python for Neovim (https://github.com/zchee/deoplete-jedi/wiki/Setting-up-Python-for-Neovim)

I’m not all clear why installing into /usr/local/bin is a risk on a Mac given the fact that the system only relies on python binaries in /Library/Frameworks/ and /usr/bin. I suspect it’s because as noted above, installing into /usr/local/bin requires sudo which opens the door to making a costly mistake with the system libraries. Thus, installing into ~/.local/bin is a sure fire way to avoid this risk.

Ref: Using python on a Mac (https://docs.python.org/2/using/mac.html)

Finally, to the degree there is a benefit of installing packages into the /usr/local/bin, I wonder if it makes sense to change the owner of the directory from root to user? This would avoid having to use sudo while still protecting against making system-dependent changes.* Is this a security default a relic of how Unix systems were more often used in the past (as servers)? Or at minimum, just a good way to go for Mac users not hosting a server?

*Note: Mac’s System Integrity Protection (SIP) feature also seems to protect the user from changing the system-dependent libraries.

– E


回答 6

没有虚拟环境

pip <command> --user 更改当前pip命令的范围,使其在当前用户帐户的本地python软件包安装位置(而不是系统范围的软件包安装位置)上起作用,这是默认设置。

  • 请参阅《 PIP用户指南》中的“ 用户安装 ”。

这仅在多用户计算机上才真正重要。安装到系统位置的所有内容都将对所有用户可见,因此,安装到该用户位置将使该软件包安装与其他用户分开(他们不会看到该软件包,因此必须自己单独安装才能使用)。因为可能存在版本冲突,所以安装具有其他软件包所需要的依赖关系的软件包可能会引起问题,因此最好不要将给定用户使用的所有软件包推送到系统安装位置。

  • 如果是单用户计算机,则安装到该--user位置几乎没有差异。它将被安装到一个不同的文件夹中,该文件夹可能或不需要添加到路径中,具体取决于软件包及其使用方式(许多软件包都安装了命令行工具,这些工具必须位于要从外壳程序运行的路径上) 。
  • 如果它是多用户计算机,--user则首选使用root / sudo或要求管理员安装并影响每个用户的Python环境,除非管理员希望默认情况下使所有用户都能使用常规软件包。
    • 注意: 根据评论,在大多数Unix / Linux安装中,已经指出系统安装应使用常规的软件包管理器,例如apt,而不是pip

使用虚拟环境

--user在活动的venv / virtualenv环境中,该选项将安装到本地用户python位置(与没有虚拟环境时相同)。

默认情况下,软件包已安装到虚拟环境,但是如果使用--user它,将强制将其安装在虚拟环境之外的用户python脚本目录中(在Windows中,当前c:\users\<username>\appdata\roaming\python\python37\scripts适用于Python 3.7)。

但是,您将无法从虚拟环境中访问系统或用户安装(即使您--user在虚拟环境中使用过)。

如果使用--system-site-packages参数安装虚拟环境,则可以访问python的系统脚本文件夹。我相信这也包括用户python脚本文件夹,但是我不确定。但是,这样做可能会产生意想不到的后果,并且它不是使用虚拟环境的预期方式。


Python系统和本地用户安装文件夹的位置

您可以使用找到python用户安装文件夹的位置python -m site --user-base。我在“问题与解答”,文档中发现有冲突的信息,并且实际上在PC上使用此命令来确定默认值,但它们位于用户主目录(~* nix中的快捷方式,c:\users\<username>通常用于Windows)下。


其他详情

--user选项并非对每个命令都有效。例如,pip uninstall将在安装了软件包的位置(用户文件夹,虚拟环境文件夹等)中查找和卸载软件包,并且该--user选项无效。

随同pip install --user安装的东西将安装在本地位置,该位置只能由当前用户帐户查看,并且不需要root用户访问权限(在* nix上)或管理员访问权限(在Windows上)。

--user选项会修改所有 pip接受该命令的命令,以便在用户安装文件夹中查看/操作,因此,如果使用pip list --user该选项,则只会显示您使用进行安装的软件包pip install --user

Without Virtual Environments

pip <command> --user changes the scope of the current pip command to work on the current user account’s local python package install location, rather than the system-wide package install location, which is the default.

This only really matters on a multi-user machine. Anything installed to the system location will be visible to all users, so installing to the user location will keep that package installation separate from other users (they will not see it, and would have to install it themselves separately to use it). Because there can be version conflicts, installing a package with dependencies needed by other packages can cause problems, so it’s best not to push all packages a given user uses to the system install location.

  • If it is a single-user machine, there is little or no difference to installing to the --user location. It will be installed to a different folder, that may or may not need to be added to the path, depending on the package and how it’s used (many packages install command-line tools that must be on the path to run from a shell).
  • If it is a multi-user machine, --user is preferred to using root/sudo or requiring administrator installation and affecting the Python environment of every user, except in cases of general packages that the administrator wants to make available to all users by default.
    • Note: Per comments, on most Unix/Linux installs it has been pointed out that system installs should use the general package manager, such as apt, rather than pip.

With Virtual Environments

The --user option in an active venv/virtualenv environment will install to the local user python location (same as without a virtual environment).

Packages are installed to the virtual environment by default, but if you use --user it will force it to install outside the virtual environments, in the users python script directory (in Windows, this currently is c:\users\<username>\appdata\roaming\python\python37\scripts for me with Python 3.7).

However, you won’t be able to access a system or user install from within virtual environment (even if you used --user while in a virtual environment).

If you install a virtual environment with the --system-site-packages argument, you will have access to the system script folder for python. I believe this included the user python script folder as well, but I’m unsure. However, there may be unintended consequences for this and it is not the intended way to use virtual environments.


Location of the Python System and Local User Install Folders

You can find the location of the user install folder for python with python -m site --user-base. I’m finding conflicting information in Q&A’s, the documentation and actually using this command on my PC as to what the defaults are, but they are underneath the user home directory (~ shortcut in *nix, and c:\users\<username> typically for Windows).


Other Details

The --user option is not a valid for every command. For example pip uninstall will find and uninstall packages wherever they were installed (in the user folder, virtual environment folder, etc.) and the --user option is not valid.

Things installed with pip install --user will be installed in a local location that will only be seen by the current user account, and will not require root access (on *nix) or administrator access (on Windows).

The --user option modifies all pip commands that accept it to see/operate on the user install folder, so if you use pip list --user it will only show you packages installed with pip install --user.


回答 7

为什么不将可执行文件放在$ PATH中

~/.local/bin directory从理论上讲应该在您的计算机中$PATH

根据这些人 说法,$PATH在使用时不添加它是一个错误systemd

该答案对其进行了更广泛的解释。

但是,即使你的发行版包括~/.local/bin目录$PATH,它可能是以下形式(内~/.profile):

if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

如果该目录以前不在目录中,则需要注销并再次登录

Why not just put an executable somewhere in my $PATH

~/.local/bin directory is theoretically expected to be in your $PATH.

According to these people it’s a bug not adding it in the $PATH when using systemd.

This answer explains it more extensively.

But even if your distro includes the ~/.local/bin directory to the $PATH, it might be in the following form (inside ~/.profile):

if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

which would require you to logout and login again, if the directory was not there before.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。