使用virtualenv,我使用默认版本的Python(2.7)运行我的项目。在一个项目中,我需要使用Python 3.4。
我使用brew install python3在Mac上安装它。现在,如何创建使用新版本的virtualenv?
例如sudo virtualenv envPython3
如果我尝试:
virtualenv -p python3 test
我得到:
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.0_1/Frameworks/Python.framework/Versions/3.4'
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Failed to import the site module
Traceback (most recent call last):
File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/site.py", line 67, in <module>
import os
File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/os.py", line 634, in <module>
from _collections_abc import MutableMapping
ImportError: No module named '_collections_abc'
ERROR: The executable test/bin/python3.4 is not functioning
ERROR: It thinks sys.prefix is '/Users/user/Documents/workspace/test' (should be '/Users/user/Documents/workspace/test/test')
ERROR: virtualenv is not compatible with this system or executable
我尝试过pyenv,它非常方便地切换python版本(全局版本、本地文件夹版本或虚拟版本):
brew install pyenv
然后安装所需的Python版本:
pyenv install 3.5.0
并简单地创建virtualenv,其中包含所需解释器版本的路径:
virtualenv -p /Users/johnny/.pyenv/versions/3.5.0/bin/python3.5 myenv
就是这样,检查版本:
. ./myenv/bin/activate && python -V
还有pyenv pyenv virtualenv的插件,但不知怎么的,它不适合我。
您可以在创建环境时指定Python的特定版本。virtualenv.py中提到过
virtualenv --python=python3.5 envname
在某些情况下,这必须是可执行文件的完整路径:
virtualenv --python=/Users/username/.pyenv/versions/3.6.0/bin/python3.6 envname
-p的工作原理
parser.add_option(
'-p', '--python',
dest='python',
metavar='PYTHON_EXE',
help='The Python interpreter to use, e.g., --python=python3.5 will use the python3.5 '
'interpreter to create the new environment. The default is the interpreter that '
'virtualenv was installed with (%s)' % sys.executable)
我尝试了以上所有的方法,但仍然没有奏效。所以作为一个蛮力,我只是重新安装了水蟒,重新安装了虚拟。。。并且成功了。
Amans-MacBook-Pro:~ amanmadan$ pip install virtualenv
You are using pip version 6.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting virtualenv
Downloading virtualenv-15.0.3-py2.py3-none-any.whl (3.5MB)
100% |████████████████████████████████| 3.5MB 114kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.3
Amans-MacBook-Pro:python amanmadan$ virtualenv my_env
New python executable in /Users/amanmadan/Documents/HadoopStuff/python/my_env/bin/python
Installing setuptools, pip, wheel...done.
Amans-MacBook-Pro:python amanmadan$