Python 3 virtualenv 中的 pip 安装错误 [英] pip install error within Python 3 virtualenv

查看:107
本文介绍了Python 3 virtualenv 中的 pip 安装错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Python 3 virtualenv,如下所示:

I created a Python 3 virtualenv, like this:

mkproject -p python3 flowerid

但是当我尝试使用 pip 安装任何东西(在这个 virtualenv 中)时,我收到了这个错误:

But when I try to install anything with pip (inside this virtualenv) I get this error:

cd flowerid
pip install ipython
Traceback (most recent call last):
  File "/Users/nicolas/.virtualenvs/flowerid/bin/pip", line 11, in <module>
    load_entry_point('pip==9.0.1', 'console_scripts', 'pip')()
  File "/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/pkg_resources/__init__.py", line 560, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2648, in load_entry_point
    return ep.load()
  File "/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2302, in load
    return self.resolve()
  File "/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2308, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/pip/__init__.py", line 28, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/pip/vcs/subversion.py", line 9, in <module>
    from pip.index import Link
  File "/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/pip/index.py", line 31, in <module>
    from pip.wheel import Wheel, wheel_ext
  File "/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/pip/wheel.py", line 6, in <module>
    import compileall
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/compileall.py", line 20, in <module>
    from concurrent.futures import ProcessPoolExecutor
  File "/Library/Python/2.7/site-packages/concurrent/futures/__init__.py", line 8, in <module>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "/Library/Python/2.7/site-packages/concurrent/futures/_base.py", line 357
    raise type(self._exception), self._exception, self._traceback
                               ^
SyntaxError: invalid syntax

一切看起来都很正常,直到进入/Library/Python/2.7"的期货"导入.

Everything looks normal until the 'futures' import that goes to "/Library/Python/2.7".

我在创建 virtualenv 时尝试设置--no-site-packages",但这不会改变任何东西(它不应该是默认设置).

I tried to set "--no-site-packages" when creating the virtualenv, but that doesn't change anything (and it shouldn't as it's the default).

我尝试改用 pip3,同样的事情.

I tried to use pip3 instead, same thing.

我尝试使用 easy_install pip 重新安装 pip(在 virtualenv 中),同样的事情...

I tried to reinstall pip (in the virtualenv) with easy_install pip, same thing...

还有其他想法吗?感谢您的帮助.

Any other ideas? Thanks for your help.

注意:我通常使用 Python 2.7 - 我确实在全球安装了许多 Python 2.7 包 - 也许这就是造成冲突的原因?

Note: I usually use Python 2.7 - I do have many Python 2.7 packages installed globally - perhaps that's what create the conflicts?

我使用的是 OSX El Capitan.

I'm on OSX El Capitan.

更新:我的 virtualenv 似乎已激活 - 它开始使用我的 virtualenv 中的 pip 和 python.'which python' 使用来自 virtualenv 的 python.

Update: My virtualenv seems activated - it starts off using the pip and python from my virtualenv. And 'which python' uses python from the virtualenv.

(flowerid) nicolas@~/venv_projects/flowerid$ which python
/Users/nicolas/.virtualenvs/flowerid/bin/python 

推荐答案

好的,我想通了.这是我的环境变量 PYTHONPATH - 把事情搞砸了 - 告诉 Python 查看 Python 2.7 库......我正在我的 .bashrc 中设置我的 PYTHONPATH.

Ok, I figured it out. It was my environmental variable PYTHONPATH - that was messing things up - telling Python to look in the Python 2.7 libraries... I am setting up my PYTHONPATH in my .bashrc.

所以解决方案是在激活时更改 PYTHONPATH 并在停用时设置回以前的设置(这让我有点难以置信,这不是默认设置......)

So the solution is to change the PYTHONPATH on activation and setting back the previous setting on deactivation (it boggles my mind a bit that this is not default...)

所以在 /Users/nicolas/.virtualenvs/flowerid/bin/activate 添加:

# Fix PYTHONPATH imports
export OLD_PYTHONPATH="$PYTHONPATH"
export PYTHONPATH="/Users/nicolas/.virtualenvs/flowerid/lib/python3.6/site-packages/"

/Users/nicolas/.virtualenvs/flowerid/bin/postdeactivate中:

# Reset PYTHONPATH to previous state
export PYTHONPATH="$OLD_PYTHONPATH"

在这里我发现了问题所在:virtualenv --no-site-packages 和 pip 仍在寻找全局包?

Here's where I found out what the problem was: virtualenv --no-site-packages and pip still finding global packages?

解决办法:您如何在已经-创建 virtualenv?

这篇关于Python 3 virtualenv 中的 pip 安装错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆