Python 如何在全局而不是本地安装 pip 包?

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

How do I install a pip package globally instead of locally?

pythonpip

提问by sorin

I am trying to install flake8 package using pip3 and it seems that it refuses to install because is already installed in one local location.

我正在尝试使用 pip3 安装 flake8 软件包,但它似乎拒绝安装,因为它已安装在一个本地位置。

How can I force it to install globally (system level)?

如何强制它全局安装(系统级别)?

pip3 install flake8
Requirement already satisfied (use --upgrade to upgrade): flake8 in ./.local/lib/python3.4/site-packages

Please note that I would prefer a generic solution (that should work on Debian, OS X maybe even Windows), one that should be used on any platform so I don't want to specify the destination myself.

请注意,我更喜欢通用解决方案(应该在 Debian、OS X 甚至 Windows 上工作),应该在任何平台上使用,所以我不想自己指定目标。

For some weird reason it behaves like I already specified --userwhich in my case I didn't.

出于某种奇怪的原因,它的行为就像我已经指定的那样--user,在我的情况下我没有指定。

The only way I was able to install a package globally was to first remove it and install it again after this. Somehow it seems that pip (8.1.1) refuses to install a package globally if it exists locally.

我能够全局安装软件包的唯一方法是先将其删除,然后再重新安装。不知何故,pip (8.1.1) 似乎拒绝在本地安装一个包,如果它存在于本地。

Disclaimer: No virtual environments were used or harmed during the experiments.

免责声明:在实验过程中没有使用或损害虚拟环境。

回答by nikhilweee

Why don't you try sudowith the Hflag? This should do the trick.

你为什么不尝试sudoH标志?这应该可以解决问题。

sudo -H pip install flake8

A regular sudo pip install flake8will try to use your own home directory. The -Hinstructs it to use the system's home directory. More info at https://stackoverflow.com/a/43623102/

常客sudo pip install flake8会尝试使用您自己的主目录。该-H指示它使用系统的主目录。更多信息请访问https://stackoverflow.com/a/43623102/

回答by Manuel

Maybe --force-reinstallwould work, otherwise --ignore-installedshould do the trick.

也许--force-reinstall会起作用,否则--ignore-installed应该可以解决问题。

回答by Mauro Baraldi

Are you using virtualenv? If yes, deactivate the virtualenv. If you are not using, it is already installed widely (system level). Try to upgrade package.

你在用virtualenv吗?如果是,请停用 virtualenv。如果你没有使用,它已经被广泛安装(系统级)。尝试升级包。

pip install flake8 --upgrade

回答by LIT

I actually don‘t see your issue. Globally is any package which is in your python3 path‘s site package folder.

我实际上没有看到你的问题。全局是在您的 python3 路径的站点包文件夹中的任何包。

If you want to use it just locally then you must configure a virtualenv and reinstall the packages with an activated virtual environment.

如果您只想在本地使用它,那么您必须配置一个 virtualenv 并使用激活的虚拟环境重新安装软件包。