如何指定用于创建虚拟环境的python版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45293436/
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
How to specify python version used to create Virtual Environment?
提问by Danoram
My Python virtual environments use python3.6
when I create them using virtualenv
我的 Python 虚拟环境python3.6
在创建它们时使用virtualenv
~ $ virtualenv my_env
~ $ virtualenv my_env
but I need to use python3.5
as 3.6 is not currently supported by Opencv3
.
但我需要使用python3.5
3.6 目前不支持Opencv3
。
I've tried using the --python=<py_version>
flag when creating a virtual environment but this doesn't work.
我--python=<py_version>
在创建虚拟环境时尝试使用该标志,但这不起作用。
How do I specify the python (3.x) version to install using virtualenv
for Mac and/or Linux?
如何virtualenv
为 Mac 和/或 Linux指定要安装的 python (3.x) 版本?
采纳答案by nehem
Assuming that you have installed python3
or any desired version of Python (2.6, 2.7, 3.5, 3.6), Now while creating the virtual environment directly pass the python executable path. Hence here are few valid example
假设你已经安装python3
或任何想要的 Python 版本(2.6、2.7、3.5、3.6),现在在创建虚拟环境时直接传递 python 可执行路径。因此这里有几个有效的例子
$ virtualenv new_p2_env # Creates a new default python environment (usually python 2)
$ virtualenv -p python3 new_p3_env # Creates a new default python3 (python3 must be a valid command i.e found in the PATH)
And last
最后
# Directly point to any version of python binary, this can be even another virtualenv's bin/python.
$ virtualenv -p /path/to/any/bin/python new_env
回答by Mariano Anaya
Alternatively, I think you could use the specific version of Python itself to create the virtual environment. That way, you'll know for sure it's the correct version:
或者,我认为您可以使用 Python 本身的特定版本来创建虚拟环境。这样,您就可以确定它是正确的版本:
$ python3.5 -m venv test35
$ ./test35/bin/python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build ) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Reference at https://docs.python.org/3.5/library/venv.html
回答by Thomas
As of version 3.3, python includes a package named venv
. However that package doesn't provide the same functionalities as the traditional virtualenv
package.
从 3.3 版开始,python 包含一个名为venv
. 但是,该软件包不提供与传统virtualenv
软件包相同的功能。
venv
allows creating virtual environments only for the version of python it's installed for.
virtualenv
allows creating virtual environments for different versions of python by providing the path to the binary.
venv
只允许为它安装的 python 版本创建虚拟环境。
virtualenv
允许通过提供二进制文件的路径为不同版本的 python 创建虚拟环境。
Creating virtual envs for different versions of python:
为不同版本的 python 创建虚拟环境:
So assuming one has python 2.7 and python 3.6 installed in /path/to/
and wants to create the virtual env named respectively env-py36
with python 3.6 and env-py27
with python 2.7
所以假设一个人安装了 python 2.7 和 python 3.6/path/to/
并且想要创建分别env-py36
用 python 3.6 和env-py27
python 2.7命名的虚拟环境
# create a virtual env with python3's venv :
/path/to/python36/bin/python3 -m venv /my/python-venvs/env-py36
. /my/python-venvs/env-py36/bin/activate
# we're now running python 3's "env-py36" virtual env, we want to install the "virtualenv" package
pip install virtualenv
deactivate
# now use virtualenv to create a virtual environment for python 2.7
/my/python-venvs/env-py36/bin/virtualenv --python=/path/to/python27/bin/python /my/python-venvs/env-py27
Using python 3.3+ venv
使用 python 3.3+ venv
Python 3.3+ :
Python 3.3+:
/path/to/python3/bin/python3 -m venv ENV_DIR
Python 3.3 to 3.5 (deprecated in 3.6+) :
Python 3.3 到 3.5(在 3.6+ 中已弃用):
/path/to/python3/bin/pyvenv ENV_DIR
Sources:
资料来源:
回答by Viktor
I working on all ubuntu and MacOS
我在所有 ubuntu 和 MacOS 上工作
Ubuntu :
virtualenv -p python3.6 environment_file
Mac OS :
virtualenv -p python3.6 environment_file
Ubuntu:
virtualenv -p python3.6 environment_file
苹果系统 :
virtualenv -p python3.6 environment_file
I think it be same
我认为是一样的