Python 3 virtualenv中的pip安装错误

时间:2017-05-29 10:06:58

标签: python python-2.7 python-3.x virtualenv

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

mkproject -p python3 flowerid

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

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”之前,一切看起来都很正常。

我在创建virtualenv时尝试设置“--no-site-packages”,但这并没有改变任何东西(它不应该是默认值)。

我尝试使用pip3,同样的事情。

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

还有其他想法吗?谢谢你的帮助。

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

我在OSX El Capitan。

更新 我的virtualenv似乎已激活 - 它开始使用我的virtualenv的pip和python。并且'哪个python'使用来自virtualenv的python。

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

3 个答案:

答案 0 :(得分:0)

好的,我明白了。这是我的环境变量PYTHONPATH - 这让事情搞得一团糟 - 告诉Python查看Python 2.7库......我正在我的.bashrc中设置我的PYTHONPATH。

因此,解决方案是在激活时更改PYTHONPATH并在停用时将之前的设置设置回来(这让我有点担心这不是默认设置......)

所以在/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 and pip still finding global packages?

解决方案: How do you set your pythonpath in an already-created virtualenv?

答案 1 :(得分:-1)

尝试替换/Library/Python/2.7/site-packages/concurrent/futures/_base.py

中的行
    raise type(self._exception), self._exception, self._traceback

用这个

    raise Exception(self._exception), self._exception, self._traceback

答案 2 :(得分:-1)

尝试使用pip3而不是pip命令。因为你使用的是python 3。