使用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

当前回答

这就是在python/python3中运行虚拟环境所需的全部内容

首先,如果未安装virtualenv,请运行

pip3 install virtualenv 

立即运行:

virtualenv -p python3 <env name> # you can specify full path instead <env_name> to install the files in a different location other than the current location

有时cmd virtualenv失败,如果失败,请使用以下命令:

python3 -m virtualenv <env_name>  # you can specify full path instead <env_name> to install the files in a different location other than the current location

现在激活虚拟env:

source <env_name>/bin/activate

Or:

source `pwd`/<env_name>/bin/activate

现在运行

which python

您应该看到目录的完整路径和<env_name>/bin/python后缀

要退出virtualenv,请运行:

deactivate 

要解决Python问题,请转到此处

其他回答

我收到了同样的错误,因为它与miniconda3安装冲突,所以当您键入“which virtualenv”时,如果您已经安装了miniconda,并且它指向该安装,则可以删除它(如果您喜欢我,并且还没有移动到它)或更改环境变量以指向您想要的安装。

简单运行

virtualenv -p python3 envname

OP编辑后更新:

正如这里所描述的,OP版本的virtualenv中存在一个bug。通过运行以下命令解决了问题:

pip install --upgrade virtualenv
virtualenv --python=/usr/bin/python3 <name of env>

为我工作。

我尝试了以上所有的方法,但仍然没有奏效。所以作为一个蛮力,我只是重新安装了水蟒,重新安装了虚拟。。。并且成功了。

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$ 

对于使用pipenv并希望安装特定版本的用户:

pipenv install --python 3.6