在 virtualenv 中使用 Python 3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23842713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Python 3 in virtualenv
提问by Prometheus
Using virtualenv, I run my projects with the default version of Python (2.7). On one project, I need to use Python 3.4.
使用virtualenv,我使用默认版本的 Python (2.7) 运行我的项目。在一个项目中,我需要使用 Python 3.4。
I used brew install python3
to install it on my Mac. Now, how do I create a virtualenv that uses the new version?
我曾经brew install python3
在我的Mac上安装它。现在,我如何创建一个使用新版本的 virtualenv?
e.g. 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
采纳答案by tbrisker
回答by geckon
Python 3 has a built-in support for virtual environments - venv. It might be better to use that instead. Referring to the docs:
Python 3 内置了对虚拟环境的支持 - venv。改用它可能会更好。参考文档:
Creation of virtual environments is done by executing the pyvenv script:
pyvenv /path/to/new/virtual/environment
虚拟环境的创建是通过执行 pyvenv 脚本完成的:
pyvenv /path/to/new/virtual/environment
Update for Python 3.6 and newer:
Python 3.6 及更高版本的更新:
As pawciobielcorrectly comments, pyvenv
is deprecatedas of Python 3.6 and the new way is:
作为pawciobiel正确评价,pyvenv
被弃用像Python 3.6和新的方法是:
python3 -m venv /path/to/new/virtual/environment
回答by sage
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). This was my problem.
如果这在 /usr/local/bin 中出现了一些东西,那么有可能 - 甚至可能 - 您安装了 virtualenv(可能使用 easy_tools 或 pip 的实例)而不使用系统的包管理器(在 OP 的情况下为 brew)。这是我的问题。
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 后,我的问题就消失了。
回答by Johnner
I'v tried pyenvand 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-virtualenvbut it didn't work for me somehow.
也有 pyenv pyenv-virtualenv插件,但它以某种方式对我不起作用。
回答by Dadaso Zanzane
It worked for me
它对我有用
virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
回答by bewithaman
virtualenv --python=/usr/bin/python3 <name of env>
worked for me.
为我工作。
回答by Antony
If you install python3 (brew install python3
) along with virtualenv burrito, you can then do mkvirtualenv -p $(which python3) env_name
如果你安装 python3 ( brew install python3
) 和virtualenv burrito,你就可以 mkvirtualenv -p $(which python3) env_name
Of course, I know virtualenv burrito is just a wrapper, but it has served me well over the years, reducing some learning curves.
当然,我知道 virtualenv burrito 只是一个包装器,但多年来它对我很有帮助,减少了一些学习曲线。
回答by Sandeep
Install prerequisites.
安装先决条件。
sudo apt-get install python3 python3-pip virtualenvwrapper
Create a Python3 based virtual environment. Optionally enable --system-site-packages
flag.
创建一个基于 Python3 的虚拟环境。可选启用--system-site-packages
标志。
mkvirtualenv -p /usr/bin/python3 <venv-name>
Set into the virtual environment.
进入虚拟环境。
workon <venv-name>
Install other requirements using pip
package manager.
使用pip
包管理器安装其他要求。
pip install -r requirements.txt
pip install <package_name>
When working on multiple python projects simultaneously it is usually recommended to install common packages like pdbpp
globally and then reuse them in virtualenvs.
当同时处理多个 python 项目时,通常建议安装通用包,例如pdbpp
全局安装,然后在 virtualenvs 中重用它们。
Using this technique saves a lot of time spent on fetching packages and installing them, apart from consuming minimal disk space and network bandwidth.
除了消耗最少的磁盘空间和网络带宽外,使用这种技术还可以节省大量时间用于获取软件包和安装它们。
sudo -H pip3 -v install pdbpp
mkvirtualenv -p $(which python3) --system-site-packages <venv-name>
Django specific instructions
Django 具体说明
If there are a lot of system wide python packages then it is recommended to not use --system-site-packages
flag especially during development since I have noticed that it slows down Django startup a lot. I presume Django environment initialisation is manually scanning and appending all site packages from the system path which might be the reason. Even python manage.py shell
becomes very slow.
如果有很多系统范围的 python 包,那么建议不要使用--system-site-packages
标志,尤其是在开发过程中,因为我注意到它会大大减慢 Django 的启动速度。我认为 Django 环境初始化是手动扫描和附加系统路径中的所有站点包,这可能是原因。甚至python manage.py shell
变得很慢。
Having said that experiment which option works better. Might be safe to just skip --system-site-packages
flag for Django projects.
话虽如此,实验哪个选项效果更好。跳过--system-site-packages
Django 项目的标志可能是安全的。
回答by Aman Madan
I tried all the above stuff, it still didn't work. So as a brute force, I just re-installed the anaconda, re-installed the virtualenv... and it worked.
上面的东西我都试过了,还是不行。因此,作为蛮力,我只是重新安装了 anaconda,重新安装了 virtualenv ......并且它起作用了。
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$
回答by Stryker
On Mac I had to do the following to get it to work.
在 Mac 上,我必须执行以下操作才能使其正常工作。
mkvirtualenv --python=/usr/bin/python3 YourEnvNameHere