在virtualenv中使用Python 3

本文翻译自:Using Python 3 in virtualenv

Using virtualenv , I run my projects with the default version of Python (2.7). 使用virtualenv ,我使用默认版本的Python(2.7)运行项目。 On one project, I need to use Python 3.4. 在一个项目中,我需要使用Python 3.4。

I used brew install python3 to install it on my Mac. 我使用brew install python3将其安装在Mac上。 Now, how do I create a virtualenv that uses the new version? 现在,如何创建使用新版本的virtualenv?

eg sudo virtualenv envPython3 例如sudo virtualenv envPython3

If I try: 如果我尝试:

virtualenv -p python3 test

I get: 我得到:

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

#1楼

参考:https://stackoom.com/question/1c2Zt/在virtualenv中使用Python


#2楼

simply run 只需运行

virtualenv -p python3 envname

Update after OP's edit: OP编辑后更新:

There was a bug in the OP's version of virtualenv, as described here . 有没有在OP的版本virtualenv中的一个bug,如所描述这里 The problem was fixed by running: 该问题已通过运行解决:

pip install --upgrade virtualenv

#3楼

Python 3 has a built-in support for virtual environments - venv . Python 3具有对虚拟环境venv的内置支持。 It might be better to use that instead. 最好改用它。 Referring to the docs: 参考文档:

Creation of virtual environments is done by executing the pyvenv script: 通过执行pyvenv脚本来创建虚拟环境:

pyvenv /path/to/new/virtual/environment

Update for Python 3.6 and newer: 适用于Python 3.6及更高版本的更新:

As pawciobiel correctly comments , pyvenv is deprecated as of Python 3.6 and the new way is: pawciobiel正确注释时 ,从Python 3.6开始不推荐使用 pyvenv ,新方法是:

python3 -m venv /path/to/new/virtual/environment

#4楼

In addition to the other answers, I recommend checking what instance of virtualenv you are executing: 除了其他答案,我建议检查您正在执行哪个virtualenv实例:

which virtualenv

If this turns up something in /usr/local/bin, then it is possible - even likely - that you installed virtualenv (possibly using an instance of easy_tools or pip) without using your system's package manager (brew in OP's case). 如果在/ usr / local / bin中出现问题,则可能甚至可能安装了virtualenv(可能使用easy_tools或pip实例)而没有使用系统的程序包管理器(在OP中为棕色)。 This was my problem. 这是我的问题。

Years ago - when I was even more ignorant - I had installed virtualenv and it was masking my system's package-provided virtualenv. 多年前-当我更加无知的时候-我安装了virtualenv,它掩盖了我系统的软件包提供的virtualenv。

After removing this old, broken virtualenv, my problems went away. 删除了这个破旧的virtualenv之后,我的问题就消失了。


#5楼

I'v tried pyenv and it's very handy for switching python versions (global, local in folder or in the virtualenv): 我尝试过pyenv ,它对于切换python版本(全局,文件夹或virtualenv中的本地)非常方便:

brew install pyenv

then install Python version you want: 然后安装所需的Python版本:

pyenv install 3.5.0

and simply create virtualenv with path to needed interpreter version: 并只需创建virtualenv并包含所需解释器版本的路径即可:

virtualenv -p /Users/johnny/.pyenv/versions/3.5.0/bin/python3.5 myenv

That's it, check the version: 就是这样,检查版本:

. ./myenv/bin/activate && python -V

There are also plugin for pyenv pyenv-virtualenv but it didn't work for me somehow. 也有pyenv的插件pyenv-virtualenv,但是它对我不起作用。


#6楼

对我有用

virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
发布了0 篇原创文章 · 获赞 75 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105456549