Python pip 安装在全局站点包而不是 virtualenv

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

pip installing in global site-packages instead of virtualenv

pythonmacosvirtualenvpip

提问by ???S???

Using pip3to install a package in a virtualenvcauses the package to be installed in the global site-packages folder instead of the one in the virtualenv folder. Here's how I set up Python3 and virtualenv on OS X Mavericks (10.9.1):

使用pip3在 a 中安装包virtualenv会导致包安装在全局 site-packages 文件夹中,而不是安装在 virtualenv 文件夹中。以下是我在 OS X Mavericks (10.9.1) 上设置 Python3 和 virtualenv 的方法:

I installed Python3 using Homebrew:

我使用 Homebrew 安装了 Python3:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install python3 --with-brewed-openssl

Changed the $PATHvariable in .bash_profile; added the following line:

更改了中的$PATH变量.bash_profile;添加了以下行:

export PATH=/usr/local/bin:$PATH

Running which python3returns /usr/local/bin/python3(after restarting the shell).

运行which python3返回/usr/local/bin/python3(重新启动外壳后)。

Note: which python3still returns /usr/bin/pythonthough.

注意:which python3仍然返回 /usr/bin/python虽然。

Installed virtualenvusing pip3:

安装virtualenv使用pip3

pip3 install virtualenv

Next, create a new virtualenvand activate it:

接下来,创建一个新的virtualenv并激活它:

virtualenv testpy3 -p python3
cd testpy3
source bin/activate

Note: if I don't specify -p python3, pip will be missing from the bin folder in the virtualenv.

注意:如果我不指定 -p python3,则 virtualenv 中的 bin 文件夹中将缺少 pip。

Running which pipand which pip3both return the virtualenv folder:

运行which pipwhich pip3返回 virtualenv 文件夹:

/Users/kristof/VirtualEnvs/testpy3/bin/pip3

Now, when I try to install e.g. Markdown using pip in the activated virtualenv, pip will install in the global site-packages folder instead of the site-packages folder of the virtualenv.

现在,当我尝试在激活的 virtualenv 中使用 pip 安装例如 Markdown 时,pip 将安装在全局 site-packages 文件夹中,而不是 virtualenv 的 site-packages 文件夹中。

pip install markdown

Running pip listreturns:

运行pip list返回:

Markdown (2.3.1)
pip (1.4.1)
setuptools (2.0.1)
virtualenv (1.11)

Contents of /Users/kristof/VirtualEnvs/testpy3/lib/python3.3/site-packages:

内容/Users/kristof/VirtualEnvs/testpy3/lib/python3.3/site-packages

__pycache__/
_markerlib/
easy_install.py
pip/
pip-1.5.dist-info/
pkg_resources.py
setuptools/
setuptools-2.0.2.dist-info/

Contents of /usr/local/lib/python3.3/site-packages:

内容/usr/local/lib/python3.3/site-packages

Markdown-2.3.1-py3.3.egg-info/
__pycache__/
easy-install.pth
markdown/
pip-1.4.1-py3.3.egg/
setuptools-2.0.1-py3.3.egg
setuptools.pth
virtualenv-1.11-py3.3.egg-info/
virtualenv.py
virtualenv_support/

As you can see, the globalsite-packages folder contains Markdown, the virtualenv folder doesn't.

如您所见,全局site-packages 文件夹包含 Markdown,而 virtualenv 文件夹不包含。

Note: I had Python2 and Python3 installed before on a different VM (followed theseinstructions) and had the same issue with Python3; installing packages in a Python2 based virtualenv worked flawlessly though.

注意:我之前在不同的 VM 上安装了 Python2 和 Python3(按照这些说明),并且在 Python3 上遇到了同样的问题;不过,在基于 Python2 的 virtualenv 中安装包工作得很好。

Any tips, hints, … would be very much appreciated.

任何提示、提示……将不胜感激。

采纳答案by Chase Ries

Funny you brought this up, I just had the exact same problem. I solved it eventually, but I'm still unsure as to what caused it.

有趣的是你提出了这个问题,我刚刚遇到了完全相同的问题。我最终解决了它,但我仍然不确定是什么原因造成的。

Try checking your bin/pipand bin/activatescripts. In bin/pip, look at the shebang. Is it correct? If not, correct it. Then on line ~42in your bin/activate, check to see if your virtualenv path is right. It'll look something like this

尝试检查您的bin/pipbin/activate脚本。在bin/pip,看看shebang。这是正确的吗?如果不是,请更正。然后在线 ~42在您的 中bin/activate,检查您的 virtualenv 路径是否正确。它看起来像这样

VIRTUAL_ENV="/Users/me/path/to/virtual/environment"

If it's wrong, correct it, deactivate, then . bin/activate, and if our mutual problem had the same cause, it should work. If it still doesn't, you're on the right track, anyway. I went through the same problem solving routine as you did, which piping over and over, following the stack trace, etc.

如果它是错误的,纠正它,deactivate然后. bin/activate,如果我们共同的问题有相同的原因,它应该起作用。如果它仍然没有,无论如何你都在正确的轨道上。我经历which pip了和你一样的问题解决程序,一遍又一遍,跟踪堆栈跟踪等。

Make absolutely sure that

绝对确保

/Users/kristof/VirtualEnvs/testpy3/bin/pip3

is what you want, and not referring to another similarly-named test project (I had that problem, and have no idea how it started. My suspicion is running multiple virtualenvs at the same time).

是你想要的,而不是指另一个同名的测试项目(我有这个问题,不知道它是如何开始的。我怀疑是同时运行多个 virtualenvs)。

If none of this works, a temporary solution may be to, as Joe Holloway said,

如果这些都不起作用,那么临时解决方案可能是,正如乔·霍洛威所说,

Just run the virtualenv's pip with its full path (i.e. don't rely on searching the executable path) and you don't even need to activate the environment. It will do the right thing.

只需使用完整路径运行 virtualenv 的 pip(即不要依赖于搜索可执行路径),您甚至不需要激活环境。它会做正确的事情。

Perhaps not ideal, but it ought to work in a pinch.

也许不理想,但它应该在紧要关头工作。

Link to my original question:

链接到我的原始问题:

VirtualEnv/Pip trying to install packages globally

VirtualEnv/Pip 尝试全局安装软件包

回答by kme757

I had this problem too. Calling pip install <package_name>from the /bindirectory within my Python 3.3 virtual environment on my Mavericks Mac caused the Python package to be installed in the Python 2.7 global site packages directory. This was despite the fact that my $PATH started with the directory containing pip. Weird. This doesn't happen on CentOS. For me, the solution was calling pip3instead of pip. When I had installed pipwithin the virtual environment via ez_setup, three "pip" executables had been installed in the /bindirectory - pip, pip3, and pip3.3. Curiously, all three files were exactly the same. Calling pip3 install <package_name>caused the Python package to be installed correctly into the local site-packages directory. Calling pipwith the full pathname into the virtual environment also worked correctly. I'd be interested to know why my Mac isn't using $PATH the way I would expect it to.

我也有这个问题。pip install <package_name>/bin我的 Mavericks Mac 上的 Python 3.3 虚拟环境中的目录调用导致 Python 包安装在 Python 2.7 全局站点包目录中。尽管事实上我的 $PATH 以包含pip. 奇怪的。这不会发生在 CentOS 上。对我来说,解决方案是调用pip3而不是pip. 当我已经安装了PIP通过在虚拟环境中ez_setup,三“画中画”的可执行文件已被安装在/bin目录- pippip3pip3.3。奇怪的是,这三个文件完全一样。打电话pip3 install <package_name>导致 Python 包正确安装到本地 site-packages 目录中。pip使用完整路径名调用虚拟环境也可以正常工作。我很想知道为什么我的 Mac 没有像我期望的那样使用 $PATH 。

回答by e.thompsy

For me this was not a pip or virtualenv problem. It was a python problem. I had set my $PYTHONPATH manually in ~/.bash_profile (or ~/.bashrc) after following some tutorial online. This manually set $PYTHONPATH was available in the virtualenv as it probably should be allowed.

对我来说,这不是 pip 或 virtualenv 问题。这是一个python问题。在学习了一些在线教程后,我在 ~/.bash_profile(或 ~/.bashrc)中手动设置了我的 $PYTHONPATH。这个手动设置的 $PYTHONPATH 在 vi​​rtualenv 中可用,因为它可能应该被允许。

Additionally add2virtualenvwas not adding my project path to my $PYTHONPATH for some reason within the virtualenv.

此外add2virtualenv,由于某些原因,我没有将我的项目路径添加到我的 $PYTHONPATH 中。

Just some forking paths for those who might still be stuck! Cheers!

对于那些可能仍然被卡住的人来说,只是一些分叉路径!干杯!

回答by Alok Wadhwa

I hit into the same issue while installing a python package from within a virtualenv. The root cause in my case was different. From within the virtualenv, I was (out of habit on Ubuntu), doing:

我在从 virtualenv 中安装 python 包时遇到了同样的问题。在我的案例中,根本原因是不同的。在 virtualenv 中,我(出于对 Ubuntu 的习惯)执行以下操作:

sudo easy_install -Z <package>

This caused the bin/pip shebang to be ignored and it used the root's non virtualenv python to install it in the global site-packages. Since we have a virtual environment, we should install the package without "sudo"

这导致 bin/pip shebang 被忽略,并使用 root 的非 virtualenv python 将其安装在全局站点包中。由于我们有一个虚拟环境,我们应该安装没有“sudo”的包

回答by Petru

I had this problem too. Calling sudo pip installcaused Python packages to be installed in the global site-packages diretory and calling pip installjust worked fine. So no use sudoin virtualenv.

我也有这个问题。调用sudo pip install导致 Python 包安装在全局站点包目录中,并且调用pip install工作正常。所以不要在 virtualenv 中使用sudo

回答by Erion S

Came across the same issue today. I simply reinstalled pip globally with sudo easy_install pip(OSX/ Max), then created my virtualenv again with sudo virtualenv nameOfVEnv. Then after activating the new virtualenv the pipcommand worked as expected.

今天遇到了同样的问题。我只是用sudo easy_install pip(OSX/Max)在全局重新安装了 pip ,然后用sudo virtualenv nameOfVEnv. 然后在激活新的 virtualenv 后,该pip命令按预期工作。

I don't think I used sudoon the first virtualenv creation and that may have been the reason for not having access to pipfrom within the virtualenv, I was able to get access to pip2before this fix though which was odd.

我不认为我sudo在第一个 virtualenv 创建时使用过,这可能是无法pip从 virtualenv 内部访问的原因,我能够pip2在此修复之前访问,尽管这很奇怪。

回答by Josiah Ruddell

I had a similar problem after updating to pip==8.0.0. Had to resort to debugging pip to trace out the bad path.

更新到pip==8.0.0. 不得不求助于调试 pip 来找出错误的路径。

As it turns out my profile directory had a distutils configuration file with some empty path values. This was causing all packages to be installed to the same root directory instead of the appropriate virtual environment (in my case /lib/site-packages).

事实证明,我的配置文件目录有一个带有一些空路径值的 distutils 配置文件。这导致所有软件包都安装到相同的根目录而不是适当的虚拟环境(在我的情况下/lib/site-packages)。

I'm unsure how the config file got there or how it had empty values but it started after updating pip.

我不确定配置文件是如何到达那里的,或者它是如何有空值的,但它是在更新 pip 后启动的。

In case anyone else stumbles upon this same problem, simply deleting the file ~/.pydistutils.cfg(or removing the empty config path) fixed the problem in my environment because pip went back to the default distributed configuration.

如果其他人遇到同样的问题,只需删除文件~/.pydistutils.cfg(或删除空的配置路径)即可解决我的环境中的问题,因为 pip 返回到默认的分布式配置。

回答by VPfB

The same problem. Python3.5 and pip 8.0.2 installed from Linux rpm's.

同样的问题。从 Linux 安装的 Python3.5 和 pip 8.0.2 rpm

I did not find a primary cause and cannot give a proper answer. It looks like there are multiple possible causes.

我没有找到主要原因,也无法给出正确的答案。看起来有多种可能的原因。

However, I hope I can help with sharing my observation and a workaround.

但是,我希望我可以帮助分享我的观察和解决方法。

  1. pyvenvwith --system-site-packages

    • ./bindoes not contain pip, pipis available from system site packages
    • packages are installed globally (BUG?)
  2. pyvenvwithout --system-site-packages

    • pipgets installed into ./bin, but it's a different version (from ensurepip)
    • packages are installed within the virtual environment (OK)
  1. pyvenv--system-site-packages

    • ./bin不包含pippip可从系统站点包中获得
    • 软件包是全局安装的(BUG?
  2. pyvenv没有 --system-site-packages

    • pip安装到 中./bin,但它是不同的版本(来自ensurepip
    • 软件包安装在虚拟环境中(OK

Obvious workaround for pyvenvwith --system-site-packages:

pyvenvwith 的明显解决方法--system-site-packages

  • create it without the --system-site-packagesoption
  • change include-system-site-packages = falseto truein pyvenv.cfgfile
  • 在没有--system-site-packages选项的情况下创建它
  • 更改include-system-site-packages = falsetruepyvenv.cfg文件

回答by Memos

It's also worth checking that you didn't modify somehow the path to your virtualenv.

还值得检查您是否没有以某种方式修改 virtualenv 的路径。

In that case the first line in bin/pip(and the rest of the executables) would have an incorrect path.

在这种情况下,bin/pip(和其余的可执行文件)中的第一行将具有不正确的路径。

You can either edit these files and fix the path or remove and install again the virtualenv.

您可以编辑这些文件并修复路径,也可以删除并重新安装 virtualenv。

回答by ham-sandwich

For Python 3ers

对于 Python 3ers

Try updating. I had this exact same problem and tried Chases' answer, however no success. The quickest way to refactor this is to update your Python Minor / Patch version if possible. I noticed that I was running 3.5.1 and updated to 3.5.2. Pyvenv once again works.

尝试更新。我遇到了完全相同的问题并尝试了 Chases 的回答,但没有成功。重构它的最快方法是尽可能更新您的 Python Minor / Patch 版本。我注意到我正在运行 3.5.1 并更新到 3.5.2。Pyvenv 再次起作用。