pip 不会使用 --user 在本地安装 Python 包

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

pip won't install Python packages locally with --user

pythonpackagepipeasy-installpythonpath

提问by

I'm trying to install packages locally with pip. It used to work with --userbut now when I try it, it finds the version of the package in /usr/local/lib/and then does not install it locally. Normally it would install things in ~/.localbut now it just checks the system-wide dir for the package and if it's there, it does not install it (which is not what I want) and if it's not there, it tries to install it in /usr/local/libwhich I do not have write permissions at. Eg:

我正在尝试使用 pip 在本地安装软件包。它曾经可以使用,--user但现在当我尝试它时,它会在其中找到软件包的版本,/usr/local/lib/然后不在本地安装它。通常情况下,将安装在的东西~/.local,但现在它只是检查包装上的系统级目录,如果它的存在,它不安装它(这是不是我想要的),如果它不存在,它会尝试安装它在/usr/local/lib其中我没有写权限。例如:

$ pip install --user rpy2
Requirement already satisfied (use --upgrade to upgrade): rpy2 in /usr/local/lib/python2.7/dist-packages/

How can I make pip install --useralways go to ~/.localand not a system-wide directory?

我怎样才能pip install --user总是转到~/.local而不是系统范围的目录?

回答by JamCon

According to the pip documentation, that syntax is correct, but requires Python 2.6.

根据 pip 文档,该语法是正确的,但需要 Python 2.6。

User Installs

With Python 2.6 came the “user scheme” for installation, which means that all Python distributions support an alternative install location that is specific to a user. The default location for each OS is explained in the python documentation for the site.USER_BASE variable. This mode of installation can be turned on by specifying the –user option to pip install.

Moreover, the “user scheme” can be customized by setting the PYTHONUSERBASE environment variable, which updates the value of site.USER_BASE.

To install “SomePackage” into an environment with site.USER_BASE customized to ‘/myappenv', do the following:

用户安装

Python 2.6 提供了用于安装的“用户方案”,这意味着所有 Python 发行版都支持特定于用户的备用安装位置。每个操作系统的默认位置在 site.USER_BASE 变量的 python 文档中进行了解释。这种安装模式可以通过指定 pip install 的 –user 选项来开启。

此外,可以通过设置 PYTHONUSERBASE 环境变量来自定义“用户方案”,该变量会更新 site.USER_BASE 的值。

要将“SomePackage”安装到 site.USER_BASE 自定义为“/myappenv”的环境中,请执行以下操作:

export PYTHONUSERBASE=/myappenv 
pip install --user SomePackage



So the following entry should work for you:



因此,以下条目应该适合您:

export PYTHONUSERBASE=~/.local
pip install --user rpy2

回答by Piotr Dobrogost

CitingMarcus Smith (maintainer of pip):

引用Marcus Smith(pip 的维护者):

If you think the global site is out of date, and want the latest in the user site, then use:
pip install --upgrade --user SomePackage

If the global site is up to date, and you really just want the same thing duplicated in --user, then use:
pip install --ignore-installed --user SomePackage(which works correctly now after the merge of #1352, which is to be released in v1.5)

如果您认为全局站点已过时,并且想要用户站点中的最新版本,请使用:
pip install --upgrade --user SomePackage

如果全球网站是最新的,你真的只是想同样的事情重复的--user,然后使用:
pip install --ignore-installed --user SomePackage(其中#1352合并后,现在可以正常工作,这是在1.5版被释放)

How can I make pip install --useralways go to ~/.localand not a system-wide directory?

我怎样才能pip install --user总是转到~/.local而不是系统范围的目录?

Use both --upgradeand --ignore-installedarguments.

使用--upgrade--ignore-installed参数。

回答by Bachi

installing any package using user command

使用用户命令安装任何软件包

for upgrading pip :

升级 pip :

python -m pip install --upgrade --user pip