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

当前回答

virtualenv --python=/usr/bin/python3 <name of 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问题,请转到此处

在蟒蛇3.6我试过python3-m venv myenv,根据文件,但需要很长时间。因此,非常简单和快速的命令是python-m venv yourenv我在蟒蛇上成功了3.6。

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

为我工作。

这对我有用

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

virtualenv--python=/usr/local/bin/python3<VIRTUAL ENV NAME>这将添加python3虚拟环境的路径。