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

当前回答

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

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$ 

其他回答

以下简单命令可以创建3.5版的虚拟env

apt-get install python3-venv

python3.5 -m venv <your env name>

如果希望虚拟env版本为3.6

python3.6 -m venv <your env name>

我收到了同样的错误消息。tbrisker的解决方案在我的情况下不起作用。相反,这解决了问题:

$ python3 -m venv .env

这就是在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问题,请转到此处

对于那些在使用Anaconda3(Python 3)时遇到问题的人。

你可以使用

conda create -n name_of_your_virtualenv python=python_version 

激活环境(Linux、MacOS)

source activate name_of_your_virtualenv

对于Windows

activate name_of_your_virtualenv

这对我有用

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