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
What is the purpose of "pip install --user ..."?
提问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 --user
in 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访问权限。
--user
makes pip install packages in your home directory instead, which doesn't require any special privileges.
--user
而是在您的主目录中制作 pip install 软件包,这不需要任何特殊权限。
回答by Roozbeh Zabihollahi
--user
installs 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_profile
file):
就我而言,它是/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_SITE
as 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, --user
is 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_pkg
inside a virtual environment, otherwise, virtual environment's pip
will be confused. See this answerfor more details.
所以不要pip install --user some_pkg
在虚拟环境中使用,否则虚拟环境pip
会被混淆。有关更多详细信息,请参阅此答案。
回答by user603749
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.
最好的方法是安装virtualenv
而不需要--user
混淆。您将获得更大的灵活性,而不必担心每次 pip 安装软件包时都会破坏不同的 Python 版本和项目。
回答by Edmund's Echo
On macOS, the reason for using the --user
flag 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/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.
我不是所有清楚为什么安装到/usr/local/bin
是鉴于该系统只依赖于蟒蛇的二进制文件在Mac上的危险/Library/Frameworks/
和/usr/bin
。我怀疑这是因为如上所述,安装到/usr/local/bin
requiressudo
这为系统库犯下代价高昂的错误打开了大门。因此,安装到~/.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 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?
最后,程度有安装软件包到的好处/usr/local/bin
,我不知道它是有道理的,从改变目录的所有者root
来user
? 这将避免必须使用,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> --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.
pip <command> --user
将当前 pip 命令的范围更改为在当前用户帐户的本地 python 包安装位置上工作,而不是系统范围内的包安装位置,这是默认值。
- See User Installsin the PIP User Guide.
- 请参阅PIP 用户指南中的用户安装。
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 thanpip
.
- Note:Per comments, on most Unix/Linux installs it has been pointed out that system installs should use the general package manager, such as
- 如果是单用户计算机,则安装到该
--user
位置几乎没有区别。它将安装到不同的文件夹中,可能需要或可能不需要将其添加到路径中,具体取决于包及其使用方式(许多包安装命令行工具,这些工具必须位于从 shell 运行的路径上) . - 如果是多用户机器,
--user
优先使用root/sudo或者需要管理员安装,影响每个用户的Python环境,除非是管理员希望默认所有用户都可以使用的通用包。- 注意:根据评论,在大多数 Unix/Linux 安装中已经指出系统安装应该使用通用包管理器,例如
apt
, 而不是pip
。
- 注意:根据评论,在大多数 Unix/Linux 安装中已经指出系统安装应该使用通用包管理器,例如
With Virtual Environments
使用虚拟环境
- See more about installing packages with virtual environments in the Python Packaging documentation.
- Read about how to create and use virtual environments, and the
venv
command in the Python VENV docs.
- 在 Python 打包文档 中查看有关使用虚拟环境安装包的更多信息。
- 阅读有关如何创建和使用虚拟环境以及Python VENV 文档中的
venv
命令的信息。
The --user
option 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 --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).
软件包默认安装到虚拟环境中,但如果您使用--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 --user
while in a virtual environment). Although, 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.
但是,您将无法从虚拟环境中访问系统或用户安装(即使您--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 --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.
该--user
选项并非对每个命令都有效。例如,pip uninstall
将在安装包的任何位置(在用户文件夹、虚拟环境文件夹等中)查找和卸载包,并且该--user
选项无效。
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).
安装的东西pip install --user
将安装在只有当前用户帐户才能看到的本地位置,并且不需要 root 访问权限(在 *nix 上)或管理员访问(在 Windows 上)。
The --user
option modifies allpip
commands that accept it to see/operate on the user install folder, so if you use pip list -- user
it 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 directory
is theoretically expected to be in your $PATH
.
~/.local/bin directory
理论上预计会在您的$PATH
.
According to these peopleit's a bugnot adding it in the $PATH
when using systemd
.
根据这些人的说法,这是一个错误,$PATH
在使用systemd
.
This answerexplains it more extensively.
这个答案更广泛地解释了它。
But even ifyour distro includesthe ~/.local/bin
directory 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.
如果该目录以前不存在,则需要您注销并再次登录。