Python “pip install --user ...”的目的是什么?

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

What is the purpose of "pip install --user ..."?

pythonpython-3.xpipvirtualenv

提问by Rob Truxal

From pip install --help:

来自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.

site.USER_BASE 的文档是我不理解的有趣 *NIX 主题的可怕虫洞。

What is the purpose of --userin plain english?Why would intalling the package to ~/.local/matter? Why not just put an executable somewhere in my $PATH?

--user用普通英语的目的是什么?为什么安装包很~/.local/重要?为什么不把一个可执行文件放在我的 $PATH 中?

回答by duskwuff -inactive-

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

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

--usermakes pip install packages in your home directory instead, which doesn't require any special privileges.

--user而是在您的主目录中制作 pip install 软件包,这不需要任何特殊权限。

回答by Roozbeh Zabihollahi

--userinstalls in site.USER_SITE.

--user安装在site.USER_SITE.

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

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

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

回答by rgov

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

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

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

如果要将此目录添加到 shell 的搜索路径,请使用:

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

回答by YaOzI

Just a warning:

只是一个警告:

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

根据这个问题--user当前在虚拟环境中无效pip,因为用户位置对于虚拟环境没有真正意义。

So do not use pip install --user some_pkginside a virtual environment, otherwise, virtual environment's pipwill be confused. See this answerfor more details.

所以不要pip install --user some_pkg在虚拟环境中使用,否则虚拟环境pip会被混淆。有关更多详细信息,请参阅此答案

回答by user603749

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

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

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

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

回答by Edmund's Echo

On macOS, the reason for using the --userflag is to make sure we don't corrupt the libraries the OS relies on. A conservativeapproach 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...

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

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

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

I'm not allclear why installing into /usr/local/binis 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/binrequires sudowhich opens the door to making a costly mistake with the system libraries. Thus, installing into ~/.local/binis a sure fire way to avoid this risk.

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

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

参考:在 Mac 上使用 python ( 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 rootto user? This would avoid having to use sudowhile 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?

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

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

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

- E

- E

回答by LightCC

Without Virtual Environments

没有虚拟环境

pip <command> --userchanges 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.

pip <command> --user将当前 pip 命令的范围更改为在当前用户帐户的本地 python 包安装位置上工作,而不是系统范围内的包安装位置,这是默认值。

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 --userlocation. 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, --useris 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.
  • 如果是单用户计算机,则安装到该--user位置几乎没有区别。它将安装到不同的文件夹中,可能需要或可能不需要将其添加到路径中,具体取决于包及其使用方式(许多包安装命令行工具,这些工具必须位于从 shell 运行的路径上) .
  • 如果是多用户机器,--user优先使用root/sudo或者需要管理员安装,影响每个用户的Python环境,除非是管理员希望默认所有用户都可以使用的通用包。
    • 注意:根据评论,在大多数 Unix/Linux 安装中已经指出系统安装应该使用通用包管理器,例如apt, 而不是pip



With Virtual Environments

使用虚拟环境

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

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

Packages are installed to the virtual environment by default, but if you use --userit 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\scriptsfor me with Python 3.7).

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

However, you won't be able to access a system or user install from within virtual environment (even if you used --userwhile in a virtual environment). Although, if you install a virtual environment with the --system-site-packagesargument, 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.

但是,您将无法从虚拟环境中访问系统或用户安装(即使您--user在虚拟环境中使用)。虽然,如果您使用--system-site-packages参数安装虚拟环境,您将可以访问 python 的系统脚本文件夹。我相信这也包括用户 python 脚本文件夹,但我不确定。



Location of the Python System and Local User Install Folders

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

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

您可以使用python -m site --user-base. 我在问答、文档和实际在我的 PC 上使用此命令时发现了关于默认值的冲突信息,但它们位于用户主目录下(~*nix 中的快捷方式,c:\users\<username>通常用于 Windows)。



Other Details

其他详情

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

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

Things installed with pip install --userwill 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).

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

The --useroption modifies allpipcommands that accept it to see/operate on the user install folder, so if you use pip list -- userit will onlyshow you packages installed with pip install --user.

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

回答by Jannis Ioannou

Why not just put an executable somewhere in my $PATH

为什么不把一个可执行文件放在我的 $PATH 中

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

~/.local/bin directory理论上预计会在您的$PATH.

According to these peopleit's a bugnot adding it in the $PATHwhen using systemd.

根据这些人说法,是一个错误$PATH在使用systemd.

This answerexplains it more extensively.

这个答案更广泛地解释了它。

But even ifyour distro includesthe ~/.local/bindirectory to the $PATH, it might be in the following form (inside ~/.profile):

但是,即使你的发行版包括~/.local/bin目录$PATH,它可能是以下形式(内~/.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.

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