Python 3 个 virtualenv 问题

Python 3 virtualenv problems

我遇到了类似于 this post 的问题,但我已经 运行 了 virtualenv 的最新版本,而且我还得到了一个不同的 ImportError

使用 2.7 版(默认)的 virtualenv 工作正常,但我需要为另一个项目使用 python3。我在 OSX 10.10.2.

中使用 brew install python3 安装了它

当我尝试时:

> pip install --upgrade virtualenv
Requirement already up-to-date: virtualenv in /Library/Python/2.7/site-packages
Cleaning up...

> virtualenv --version
12.0.7

> virtualenv -p python3 test

我仍然得到:

Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4'
New python executable in venv/bin/python3.4
Also creating executable in venv/bin/python
Failed to import the site module
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/site.py", line 73, in <module>
    __boot()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/site.py", line 2, in __boot
    import sys, imp, os, os.path   
  File "/Users/user/Desktop/project/studyprocessor/venv/bin/../lib/python3.4/imp.py", line 22, in <module>
    from importlib import util
  File "/Users/user/Desktop/project/studyprocessor/venv/bin/../lib/python3.4/importlib/util.py", line 12, in <module>
    from contextlib import contextmanager
ImportError: No module named 'contextlib'
ERROR: The executable venv/bin/python3.4 is not functioning
ERROR: It thinks sys.prefix is '/Users/user/Desktop/project/studyprocessor' (should be '/Users/user/Desktop/project/studyprocessor/venv')
ERROR: virtualenv is not compatible with this system or executable

有什么想法吗?

您使用的pip程序对应Python2.7版本。您需要使用对应于 Python 3.x 的 pip。所以,你应该使用

pip3 install virtualenv

或者,您可以使用 venv module 在 Python 3.3+ 中创建虚拟环境,就像这样

python3 -m venv test

尝试关注

virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3

workon py3