Python 3 virtualenv问题

Python 3 virtualenv problems

我遇到了类似这篇文章的问题,但我已经在运行最新版本的virtualenv了,我也得到了一个不同的ImportError

使用版本2.7的virtualenv(默认)工作正常,但我需要使用python3为另一个项目。 我在OSX 10.10.2中使用brew install python3安装它。

当我尝试:

1
2
3
4
5
6
7
8
> 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

我还是得到:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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程序对应于Python 2.7版本。 您需要使用与Python 3.x对应的pip。 所以,你应该使用

1
pip3 install virtualenv

或者,您可以使用venv模块在Python 3.3+中创建虚拟环境,如下所示

1
python3 -m venv test


尝试下面

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

workon py3