在 virtualenv 中使用不同的 Python 版本
如何为指定版本的Python创建虚拟环境?
How do I create a virtual environment for a specified version of Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何为指定版本的Python创建虚拟环境?
How do I create a virtual environment for a specified version of Python?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
注意:对于Python 3.3+,请参阅下面 Aelfinn 的答案 。
创建 virtualenv 实例时使用
--python
(或短-p
)选项来指定要使用的 Python 可执行文件,例如:NOTE: For Python 3.3+, see The Aelfinn's answer below.
Use the
--python
(or short-p
) option when creating a virtualenv instance to specify the Python executable you want to use, e.g.:自 Python 3.3 起,文档< /a> 建议使用 stdlib 创建虚拟环境:
另外,如果我们想要特定版本的 python,比如 3.6,那么我们可以使用
确保将引用的 Python 版本与现有系统 Python 一起安装。
Since Python 3.3, the documentation suggests creating the virtual environment using stdlib:
Also, if we want a particular version of python, lets say 3.6, then we can use
Make sure to install the referenced version of Python along with your existing system Python.
有一个更简单的方法,
感谢注释,只有在系统级别安装了 python2.7 时才有效(例如 /usr/bin/python2.7)。
否则,如果您使用自制程序,您可以使用该路径来为您提供所需的内容。
您可以使用
which python
(Linux) 或py -0p
(Windows) 找到 python 安装的路径,这也适用于 python 3。
最终压缩为:
There is an easier way,
Thanks to a comment, this only works if you have python2.7 installed at the system level (e.g. /usr/bin/python2.7).
Otherwise, if you are using homebrew you can use the path to give you what you want.
You can find the path to your python installation with
which python
(Linux) orpy -0p
(Windows)This will also work with python 3.
Ultimately condensing to:
当您位于共享托管环境中并需要安装和安装时,可以遵循以下步骤。从源代码编译 Python,然后从您的 Python 版本创建 venv。对于 Python 2.7.9。您可以按照以下方式执行操作:
虚拟环境
当然,这可以适用于您想要复制您工作和部署的确切环境的任何情况。
These are the steps you can follow when you are on a shared hosting environment and need to install & compile Python from source and then create
venv
from your Python version. For Python 2.7.9. you would do something along these lines:virtual env
Naturally, this can be applicable to any situation where you want to replicate the exact environment you work and deploy on.
在 Windows 下,这对我来说是有效的:
没有
python.exe
我得到WindowsError: [Error 5] Access is returned
我已经使用 virtualenv 1.6.1 安装了 Python2.7.1,并且我想要 python 2.5.2。
Under Windows for me this works:
without the
python.exe
I gotWindowsError: [Error 5] Access is denied
I have Python2.7.1 installed with virtualenv 1.6.1, and I wanted python 2.5.2.
Mac OSX 10.6.8 (Snow Leopard):
1) 当您执行
pip install virtualenv
时,pip 命令会与您的 Python 版本之一关联,并且virtualenv
将安装到那个版本的Python。你可以看看是什么版本的Python。如果您看到类似:
then do:
您可以在输出中看到 python 版本。
默认情况下,这将是用于您创建的任何新环境的 python 版本。但是,您可以使用
-p 标志
指定计算机上安装的任何版本的 python 在新环境中使用:http://docs.python-guide.org/en/latest/dev/ virtualenvs/
virtualenv
只是将 python 从计算机上的某个位置复制到新创建的 my_env/bin/ 目录中。2) 系统 python 位于
/usr/bin
中,而我安装的各个 python 版本默认安装到:3) 我安装的各个 python 的名称如
python2.7
或python3.2
,我可以使用这些名称而不是完整路径。========VIRTUALENVWRAPPER=========
1) 我在让 virtualenvwrapper 工作时遇到了一些问题。这就是我最终放入
~/.bash_profile
中的内容:2)
-p 选项
与 virtualenvwrapper 的工作方式不同:我必须指定 python 解释器的完整路径在新环境中使用(当我不想使用默认的python版本时):与virtualenv不同,virtualenvwrapper将在$WORKON_HOME环境变量指定的位置创建环境。这将您的所有环境集中在一处。
Mac OSX 10.6.8 (Snow Leopard):
1) When you do
pip install virtualenv
, the pip command is associated with one of your python versions, andvirtualenv
gets installed into that version of python. You can doto see what version of python that is. If you see something like:
then do:
You can see the python version in the output.
By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the
-p flag
:http://docs.python-guide.org/en/latest/dev/virtualenvs/
virtualenv
just copies python from a location on your computer into the newly created my_env/bin/ directory.2) The system python is in
/usr/bin
, while the various python versions I installed were, by default, installed into:3) The various pythons I installed have names like
python2.7
orpython3.2
, and I can use those names rather than full paths.========VIRTUALENVWRAPPER=========
1) I had some problems getting virtualenvwrapper to work. This is what I ended up putting in
~/.bash_profile
:2) The
-p option
works differently with virtualenvwrapper: I have to specify the full path to the python interpreter to be used in the new environment(when I do not want to use the default python version):Unlike virtualenv, virtualenvwrapper will create the environment at the location specified by the $WORKON_HOME environment variable. That keeps all your environments in one place.
[2019 年 11 月] 我需要在基于 Python 3.8 的 Arch Linux 系统上安装 Python 3.7 环境 (env)。系统上不再有 Python 3.7,因此我无法降级 Python 来安装我需要的包。
此外,我想在虚拟环境(venv)中使用该包/Python 3.7。我就是这样做的。
下载Python版本源文件:
我从以下位置下载了Python 3.7.4源文件
到
/mnt /Vancouver/apps/python_versions/src/Python-3.7.4.tgz
然后我将该存档(源文件)提取到
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
安装:
[注意:在我的系统 env 中,不是 venv。]
检查已安装的 Python 版本:
如何为特定 Python 创建 venv版本:
创建 Python 3.7 venv [在 Python 3.8 操作环境/系统上]:
添加到
~/.bashrc
:测试 Python 3.7 venv: >
[November 2019] I needed to install a Python 3.7 environment (env) on my Python 3.8-based Arch Linux system. Python 3.7 was no longer on the system, so I could not downgrade Python, to install a package that I needed.
Furthermore, I wanted to use that package / Python 3.7 inside a virtual environment (venv). This is how I did it.
Download Python version source files:
I downloaded the Python 3.7.4 source files from
to
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4.tgz
I then extracted that archive (source files) to
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
Installation:
[Note: in my system env, not a venv.]
Examine installed Python versions:
How to create a venv for a specific Python version:
Create Python 3.7 venv [on a Python 3.8 operating env / system]:
Added to
~/.bashrc
:Test Python 3.7 venv:
假设您当前在 virtualenv 中安装了 python 2.7。但是想要使用
python3.2
,您必须更新它:然后通过以下方式激活您的virtualenv,
然后执行:
python --version< /code> 在 shell 中检查您的版本现在是否已更新。
Suppose you currently have python 2.7 installed in your virtualenv. But want to make use of
python3.2
, You would have to update this with:Then activate your virtualenv by:
and then do:
python --version
in shell to check whether your version is now updated.您应该安装该 Python 版本。如果你有的话,基本上
,使用 virtualenv,
使用 venv
上面的示例适用于 python3.8,你可以将其更改为具有不同版本的虚拟环境,因为它们安装在你的计算机中。
You should have that Python version installed. If you have it then basically,
With virtualenv,
with venv
The above examples are for python3.8, you can change it to have different versions of virtual environments given that they are installed in your computer.
这两个命令应该可以正常工作。
virtualenv -p python2 myenv
(对于 python2)virtualenv -p python3 myenv
(对于 python3)These two commands should work fine.
virtualenv -p python2 myenv
(For python2)virtualenv -p python3 myenv
(For python3)你可以使用你想要的Python版本来调用
virtualenv
。例如:或者直接指向您的 virtualenv 路径。例如对于Windows:
通过运行:
您可以看到虚拟环境中安装的Python版本
You can call
virtualenv
with python version you want. For example:Or alternatively directly point to your virtualenv path. e.g. for windows:
And by running:
you can see the python version installed in virtual environment
-p
方法效果很好,但您必须记住每次都使用它。如果您的目标通常是切换到较新版本的 Python,那么这会很痛苦,而且还可能导致错误。您的另一个选择是设置一个与
-p
执行相同操作的环境变量。通过您的~/.bashrc
文件或您管理登录会话的环境变量的任何位置进行设置:然后
virtualenv
将在您不指定时使用它 - p
在命令行上。The
-p
approach works well, but you do have to remember to use it every time. If your goal is to switch to a newer version of Python generally, that's a pain and can also lead to mistakes.Your other option is to set an environment variable that does the same thing as
-p
. Set this via your~/.bashrc
file or wherever you manage environment variables for your login sessions:Then
virtualenv
will use that any time you don't specify-p
on the command line.在 Mac 上我使用 pyenv 和 virtualenvwrapper。我必须创建一个新的虚拟环境。你需要自制程序,如果你在 Mac 上,我假设你已经安装了它,但只是为了好玩:
我还首先冻结了我的要求,这样我就可以简单地在新的 virtualenv 中重新安装:
On the mac I use pyenv and virtualenvwrapper. I had to create a new virtualenv. You need homebrew which I'll assume you've installed if you're on a mac, but just for fun:
I also froze my requirements first so i could simply reinstall in the new virtualenv with:
2024 年登陆 macOS;使用 pyenv 时:
.. 并按照安装后说明设置 pyenv,请参阅; https://github.com/pyenv/pyenv?tab=readme-ov -file#unixmacos
基本上归结为在您的
~/.bashrc
或~/.zshrc
文件中添加这些行:安装您最喜欢的 python 版本,例如 < code>3.11.6
现在,您可以在项目的同一目录中创建一个新的 python 虚拟环境,如下所示:
激活环境:
检查此 python 环境中活动的 python 版本:
停用环境:
清理env:
只需删除名称为 python env 的文件夹,因此在这种情况下:
不要在 git 中提交您的 virtualenv。为了确保这一点,请将其添加到您的 .gitignore 文件中:
还要确保该文件夹不用于扫描 linting、测试、覆盖率,因为它会减慢这些进程的速度。因此,在一个 Python 项目中,使用
black
进行 linting,使用isort
对导入进行排序,使用pytest
进行单元测试,使用poetry< /code> 用于管理依赖项,
pyproject.toml
文件将包含以下内容:一个好的做法是将您的 virtualenv 命名为
venv
或.venv
On macOS in 2024; when using pyenv:
.. and follow post install instructions to setup pyenv, see; https://github.com/pyenv/pyenv?tab=readme-ov-file#unixmacos
basically comes down to adding these lines in your
~/.bashrc
or~/.zshrc
file:Install your favorite python version, e.g.
3.11.6
Now, you can create a new python virtual environment, in the same directory of your project, like so:
Activate the env:
Check the python version active in this python env:
Deactivate the env:
Cleanup the env:
just delete the folder with the name of the python env, so in this case:
Do not commit your virtualenv in git. To make sure, add it to your
.gitignore
file:Also make sure that this folder is not used to scan for linting, testing, coverage, as it slows down those processes. So, in a Python project that uses
black
for linting,isort
for sorting imports, andpytest
for unit testing, andpoetry
for managing dependencies, apyproject.toml
file would contain this:A good practise is to name your virualenv
venv
or.venv
更简单的是,使用命令替换为您找到 python2:
virtualenv -p $(which python2)
或者使用 virtualenvwrapper 时:
mkvirtualenv - p $(which python2)
Even easier, by using command substitution to find python2 for you:
virtualenv -p $(which python2) <path/to/new/virtualenv/>
Or when using virtualenvwrapper :
mkvirtualenv -p $(which python2) <env_name>
在 Windows 上:
或者
这使用
py
启动器 这将为您找到正确的 python 可执行文件(假设您已经安装了它)。On windows:
or
This uses the
py
launcher which will find the right python executable for you (assuming you have it installed).对于 Windows 来说,这些似乎有点过于复杂。如果您在运行 python 3.3 或更高版本的 Windows 上,则可以使用 python 启动器
py
更轻松地完成此操作。只需安装不同的 python 版本,然后运行:这将使用 python
[my version]
在当前目录中创建一个名为env
的虚拟环境。举个例子:这使用 python3.7 创建了一个名为
env
的虚拟环境并激活它。不需要路径或其他复杂的东西。These seem a little overcomplicated for Windows. If you're on Windows running python 3.3 or later, you can use the python launcher
py
to do this much more easily. Simply install the different python version, then run:This will create a virtual environment called
env
in your current directory, using python[my version]
. As an example:This creates a virtual environment called
env
using python3.7 and activates it. No paths or other complex stuff required.正如多个答案中已经提到的,使用 virtualenv 是一个干净的解决方案。然而,每个人都应该意识到的一个小陷阱是,如果在 bash_aliases 中设置了 python 的别名,例如:
该别名也将在虚拟环境中使用。因此,在这种情况下,无论使用什么解释器来创建环境,在虚拟环境中运行
python -V
将始终输出3.6
:As already mentioned in multiple answers, using virtualenv is a clean solution. However a small pitfall that everyone should be aware of is that if an alias for python is set in bash_aliases like:
this alias will also be used inside the virtual environment. So in this scenario running
python -V
inside the virtual env will always output3.6
regardless of what interpreter is used to create the environment:对于 Mac(High Sierra),在 python3 上安装 virtualenv 并为 python2 创建 virtualenv:
For Mac(High Sierra), install the virtualenv on python3 and create a virtualenv for python2:
我在Windows上使用了这个答案
https://stackoverflow.com/a/22793687/15435022
I utilized this answer for Windows
https://stackoverflow.com/a/22793687/15435022
我使用 pyenv 来管理我的 python 版本。
检查您的 python 版本:
使用 venv 创建虚拟环境:
然后激活虚拟环境:
检查你的python版本:
你可能需要删除之前的虚拟环境
I use pyenv to manage my python version.
Check your python version:
Create the virtual environment with venv:
Then activate the Virtual Environment:
Check your python version:
You may need to remove the previous virtual environment
在 Linux Ubuntu 21.04(当前是 Python 3.9.5)上,我需要获得 Python 3.7.8 的 virtualenv。开始工作的完整步骤:
找到您想要的Python版本源,例如3.7.8在这里:https://www.python.org/downloads/release/python-378/
下载 Gzip 压缩的源代码 tarball
使用
tar zxvf Python-3.7.8.tgz
解压缩(修改如果与 3.7.8 不同,请按照您的版本号的要求)将解压缩的文件夹复制到 /usr/bin :
sudo cp -r Python-3.7.8 /usr/bin
如果需要,请检查内容查看到目前为止您所拥有的内容:
ls
检查您的 python 设置和报告方式:
应该全部与您的主要安装相关(对我来说是 Python 3.9.5)
要检查您的新安装:
应该是所有与您的 3.7.8 安装相关的内容
如果您想运行它进行检查,请执行以下操作:
安装 venv:
要创建 venv(可能在您的存储库中,如果是,请将 .venv 添加到 .gitignore):
要激活您的 venv:
检查您的 venv:版本:
On Linux Ubuntu 21.04 (currently Python 3.9.5) I needed to get a virtualenv of Python 3.7.8. Full steps to get working:
Find the Python version source you want, for example 3.7.8 is here: https://www.python.org/downloads/release/python-378/
Download the Gzipped source tarball
Unzip it with
tar zxvf Python-3.7.8.tgz
(amend as required with your version number if different from 3.7.8)Copy the unzipped folder to /usr/bin with:
sudo cp -r Python-3.7.8 /usr/bin
Check the contents if you wanted to see what you have so far:
ls
Check how your python is set up and reporting:
Should be all relating to your primary install (Python 3.9.5 for me)
To check your new install:
Should be all relating to your 3.7.8 install
If you want to run it to check, do:
Install venv:
To create a venv (maybe in your repo, if so, add .venv to .gitignore):
To activate your venv:
Check your version:
在Linux的Windows子系统中:
为python3创建环境:
激活它:
In windows subsystem for linux:
Create environment for python3:
Activate it:
2020 年底:
使用 virtualenv 最无缝的体验(附加好处:使用任何可能的 python 版本)将是使用 pyenv 及其(捆绑)pyenv-virtualenv 插件(参见 https://realpython.com/intro-to-pyenv/#virtual-environments-and-pyenv)
用法:
pyenv virtualenv;
安装:
curl https://pyenv.run | bash
exec $SHELL
cf https://github.com/ pyenv/pyenv-installer
话虽这么说,现在代替使用
virtualenv
(和pip
) 的最佳替代方案是 Poetry (与上面指出的pyenv
一起处理不同的 python 版本)。另一种选择,因为它由 PyPA(
pip
和 PyPI 背后的组织)直接支持,并且自 5 月底以来已重新发布(自 2018 年末以来一直没有发布......)将是 PipenvEnd of 2020:
The most seamless experience for using virtualenv (added benefit: with any possible python version) would be to use pyenv and its (bundled) pyenv-virtualenv plugin (cf https://realpython.com/intro-to-pyenv/#virtual-environments-and-pyenv)
Usage:
pyenv virtualenv <python_version> <environment_name>
Installation:
curl https://pyenv.run | bash
exec $SHELL
cf https://github.com/pyenv/pyenv-installer
That being said, nowadays the best possible alternative instead of using
virtualenv
(andpip
) would be Poetry (along withpyenv
indicated above, to handle different python versions).Another option, because it's supported directly by the PyPA (the org behind
pip
and the PyPI) and has restarted releasing since the end of May (didn't release since late 2018 prior to that...) would be Pipenv这适用于我在 Windows 10 中的使用,其中我有 Python 3.7,并且想要降级 Python 3.6.6 中的项目:
我使用“venv”创建一个名为“venv”的新环境,我从 https://www.python.org/downloads/windows/ ;安装“下载 Windows x86-64 可执行安装程序-”;然后我在要创建环境的目录中使用了以下命令行
>C:\Users\...\Python\Python36\python.exe -m venv venv
最后,我激活使用命令行的环境:
>venv\Scripts\activate.bat
并通过调用检查 python 版本:
>python --version
Python 3.6.6
This worked for my usage in Windows 10, where I have Python 3.7 and want to downgrade for a project in Python 3.6.6:
I used "venv" to create a new environment called "venv", I downloaded from https://www.python.org/downloads/windows/ ; install "Download Windows x86-64 executable installer-" ; then I used the following command line in the directory where I want to create my environment
>C:\Users\...\Python\Python36\python.exe -m venv venv
Finally, I activated the environnent using the command line:
>venv\Scripts\activate.bat
And check the python version by calling:
>python --version
Python 3.6.6
这个问题的答案不应该那么复杂...
TL,DR:
在你的系统上安装你喜欢的Python版本并使用:
=================== =========================
我使用 venv 来安装虚拟环境,
如果你尝试
which python
你会看到 which python当你说“python”时,你指的是。例如,对我来说是:结果:
/c/Program Files/Python36/python
所以,现在你有了答案!
您可以在系统上安装任何版本的 python,并同时安装多个版本。因此,例如我在这个目录中安装了Python3.7:“C:\Program Files\Python37”。
因此,现在我不再使用“python”,而是通过
/c/Program\ Files/Python37/python
指定哪个 python:(不要忘记转义路径中的空格)
就是这样!
Answer to this question shouldn't be that complicated...
TL,DR:
install as many versions of python you prefer on your system and use:
============================================
I use venv to install virtual environments with
if you try
which python
you will see which python you are referring to, when saying "python". for example, for me it is:result:
/c/Program Files/Python36/python
So, now you have the answer!
you can install any version of python on your system and have multiple of them at the same time. So, for example I installed Python3.7 in this directory: "C:\Program Files\Python37".
So, instead of using 'python' now I specify which python by
/c/Program\ Files/Python37/python
:(don't forget to escape the space in the path)
That's it!
是,上述答案是正确的,并且在基于 Unix 的系统(例如 Linux 和 Linux)上运行良好。 MAC OS X。
我尝试为 Python2 创建 virtualenv 和Python3 使用以下命令。
这里我使用了 venv2 & venv3 作为 Python2 和 Python2 的名称。分别是Python3。
Yes, the above answers are correct and works fine on Unix based systems like Linux & MAC OS X.
I tried to create virtualenv for Python2 & Python3 with the following commands.
Here I have used venv2 & venv3 as their names for Python2 & Python3 respectively.
我使用 Windows,所以我应该在 pthon 路径上使用
.exe
I use Windows so I should use
.exe
on the pthon path假设我想使用 python 3.8 并且我正在使用 MacOS。
然后,
Suppose I want to use python 3.8 and I'm using MacOS.
Then,