在 virtualenv 中使用不同的 Python 版本

发布于 2024-08-07 05:23:59 字数 28 浏览 8 评论 0原文

如何为指定版本的Python创建虚拟环境?

How do I create a virtual environment for a specified version of Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

如梦初醒的夏天 2024-08-14 05:23:59

注意:对于Python 3.3+,请参阅下面 Aelfinn 的答案


创建 virtualenv 实例时使用 --python (或短 -p)选项来指定要使用的 Python 可执行文件,例如:

virtualenv --python="/usr/bin/python2.6" "/path/to/new/virtualenv/"

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.:

virtualenv --python="/usr/bin/python2.6" "/path/to/new/virtualenv/"
香橙ぽ 2024-08-14 05:23:59

自 Python 3.3 起,文档< /a> 建议使用 stdlib 创建虚拟环境:

python3 -m venv "my_env_name"

另外,如果我们想要特定版本的 python,比如 3.6,那么我们可以使用

python3.6 -m venv "my_env_name"

确保将引用的 Python 版本与现有系统 Python 一起安装。

Since Python 3.3, the documentation suggests creating the virtual environment using stdlib:

python3 -m venv "my_env_name"

Also, if we want a particular version of python, lets say 3.6, then we can use

python3.6 -m venv "my_env_name"

Make sure to install the referenced version of Python along with your existing system Python.

述情 2024-08-14 05:23:59

有一个更简单的方法,

virtualenv venv --python=python2.7

感谢注释,只有在系统级别安装了 python2.7 时才有效(例如 /usr/bin/python2.7)。

否则,如果您使用自制程序,您可以使用该路径来为您提供所需的内容。

virtualenv venv --python=/usr/local/bin/python

您可以使用 which python (Linux) 或 py -0p (Windows) 找到 python 安装的路径,

这也适用于 python 3。

which python3
>> /usr/local/bin/python3
virtualenv venv --python=/usr/local/bin/python3

最终压缩为:

virtualenv venv -p `which python`
virtualenv venv -p `which python3`

There is an easier way,

virtualenv venv --python=python2.7

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.

virtualenv venv --python=/usr/local/bin/python

You can find the path to your python installation with which python (Linux) or py -0p (Windows)

This will also work with python 3.

which python3
>> /usr/local/bin/python3
virtualenv venv --python=/usr/local/bin/python3

Ultimately condensing to:

virtualenv venv -p `which python`
virtualenv venv -p `which python3`
雪若未夕 2024-08-14 05:23:59

当您位于共享托管环境中并需要安装和安装时,可以遵循以下步骤。从源代码编译 Python,然后从您的 Python 版本创建 venv。对于 Python 2.7.9。您可以按照以下方式执行操作:

mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install

虚拟环境

cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate   

当然,这可以适用于您想要复制您工作和部署的确切环境的任何情况。

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:

mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install

virtual env

cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate   

Naturally, this can be applicable to any situation where you want to replicate the exact environment you work and deploy on.

伴随着你 2024-08-14 05:23:59
virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>
virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>
香橙ぽ 2024-08-14 05:23:59

在 Windows 下,这对我来说是有效的:

virtualenv --python=c:\Python25\python.exe envname

没有 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:

virtualenv --python=c:\Python25\python.exe envname

without the python.exe I got WindowsError: [Error 5] Access is denied
I have Python2.7.1 installed with virtualenv 1.6.1, and I wanted python 2.5.2.

北方的韩爷 2024-08-14 05:23:59

Mac OSX 10.6.8 (Snow Leopard):

1) 当您执行 pip install virtualenv 时,pip 命令会与您的 Python 版本之一关联,并且 virtualenv 将安装到那个版本的Python。你可以

 $ which pip   

看看是什么版本的Python。如果您看到类似:

 $ which pip
 /usr/local/bin/pip

then do:

$ ls -al /usr/local/bin/pip
lrwxrwxr-x  1 root  admin  65 Apr 10  2015 /usr/local/bin/pip ->
../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip

您可以在输出中看到 python 版本。

默认情况下,这将是用于您创建的任何新环境的 python 版本。但是,您可以使用 -p 标志 指定计算机上安装的任何版本的 python 在新环境中使用:

$ virtualenv -p python3.2 my_env  
Running virtualenv with interpreter /usr/local/bin/python3.2  
New python executable in my_env/bin/python  
Installing setuptools, pip...done.  

virtualenv my_env 将在当前目录中创建一个文件夹
将包含 Python 可执行文件和 pip 的副本
[命令]您可以使用它来安装其他软件包。

http://docs.python-guide.org/en/latest/dev/ virtualenvs/

virtualenv 只是将 python 从计算机上的某个位置复制到新创建的 my_env/bin/ 目录中。

2) 系统 python 位于 /usr/bin 中,而我安装的各个 python 版本默认安装到:

 /usr/local/bin

3) 我安装的各个 python 的名称如 python2.7python3.2,我可以使用这些名称而不是完整路径。

========VIRTUALENVWRAPPER=========

1) 我在让 virtualenvwrapper 工作时遇到了一些问题。这就是我最终放入 ~/.bash_profile 中的内容:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/django_projects  #Not very important -- mkproject command uses this
#Added the following based on: 
#http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7 
#source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

2) -p 选项 与 virtualenvwrapper 的工作方式不同:我必须指定 python 解释器的完整路径在新环境中使用(当我不想使用默认的python版本时):

$ mkvirtualenv -p /usr/local/bin/python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3
New python executable in my_env/bin/python
Installing setuptools, pip...done.
Usage: source deactivate

removes the 'bin' directory of the environment activated with 'source
activate' from PATH. 

与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, and virtualenv gets installed into that version of python. You can do

 $ which pip   

to see what version of python that is. If you see something like:

 $ which pip
 /usr/local/bin/pip

then do:

$ ls -al /usr/local/bin/pip
lrwxrwxr-x  1 root  admin  65 Apr 10  2015 /usr/local/bin/pip ->
../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip

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:

$ virtualenv -p python3.2 my_env  
Running virtualenv with interpreter /usr/local/bin/python3.2  
New python executable in my_env/bin/python  
Installing setuptools, pip...done.  

virtualenv my_env will create a folder in the current directory which
will contain the Python executable files, and a copy of the pip
[command] which you can use to install other packages.

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:

 /usr/local/bin

3) The various pythons I installed have names like python2.7 or python3.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:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/django_projects  #Not very important -- mkproject command uses this
#Added the following based on: 
#http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7 
#source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh

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):

$ mkvirtualenv -p /usr/local/bin/python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3
New python executable in my_env/bin/python
Installing setuptools, pip...done.
Usage: source deactivate

removes the 'bin' directory of the environment activated with 'source
activate' from PATH. 

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.

北渚 2024-08-14 05:23:59

[2019 年 11 月] 我需要在基于 Python 3.8 的 Arch Linux 系统上安装 Python 3.7 环境 (env)。系统上不再有 Python 3.7,因此我无法降级 Python 来安装我需要的包。

此外,我想在虚拟环境(venv)中使用该包/Python 3.7。我就是这样做的。


下载Python版本源文件:

我从以下位置下载了Python 3.7.4源文件

https://www.python.org/downloads/source/

/mnt /Vancouver/apps/python_versions/src/Python-3.7.4.tgz

然后我将该存档(源文件)提取到

/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/


安装:

[注意:在我的系统 env 中,不是 venv。]

cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
time ./configure                 ## 17 sec
time make                        ## 1 min 51 sec
time sudo make install           ## 18 sec
time make clean                  ## 0.3 sec

检查已安装的 Python 版本:

$ which python
/usr/bin/python

$ python --version
Python 3.8.0

$ which python3.7
/usr/local/bin/python3.7

$ python    ## Python 3.8 [system / env]
Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

$ python3.7    ## newly-installed Python 3.7 package
Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0]
>>>

$ python3.7 --version                                                                                                 
Python 3.7.4

如何为特定 Python 创建 venv版本:

https://docs.python.org/3/tutorial/venv.html< /a>

12.2。创建虚拟环境

用于创建和管理虚拟环境的模块称为venvvenv 通常会安装您可用的最新版本的 Python。如果您的系统上有多个版本的 Python,您可以通过运行 python3 或您想要的任何版本来选择特定的 Python 版本。

要创建虚拟环境,请确定要放置它的目录,然后使用目录路径将 venv 模块作为脚本运行:

<块引用>

python3 -m venv 教程-env

这将创建 tutorial-env 目录(如果不存在),并在其中创建包含 Python 解释器副本、标准库和各种支持文件的目录。
...


创建 Python 3.7 venv [在 Python 3.8 操作环境/系统上]:

python3.7 -m venv ~/venv/py3.7      ## create Python 3.7-based venv
source ~/venv/py3.7/bin/activate    ## activate that venv
deactivate                          ## deactivate that venv (when done, there)

添加到 ~/.bashrc

alias p37='echo "   [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'

测试 Python 3.7 venv: >

$ p37                                                                                                                 
[Python 3.7 venv (source ~/venv/py3.7/bin/activate)]

(py3.7)$ python --version
Python 3.7.4

(py3.7)$ python
Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] 
>>>

[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

https://www.python.org/downloads/source/

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.]

cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
time ./configure                 ## 17 sec
time make                        ## 1 min 51 sec
time sudo make install           ## 18 sec
time make clean                  ## 0.3 sec

Examine installed Python versions:

$ which python
/usr/bin/python

$ python --version
Python 3.8.0

$ which python3.7
/usr/local/bin/python3.7

$ python    ## Python 3.8 [system / env]
Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

$ python3.7    ## newly-installed Python 3.7 package
Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0]
>>>

$ python3.7 --version                                                                                                 
Python 3.7.4

How to create a venv for a specific Python version:

https://docs.python.org/3/tutorial/venv.html

12.2. CREATING VIRTUAL ENVIRONMENTS

The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.

To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:

python3 -m venv tutorial-env

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
...


Create Python 3.7 venv [on a Python 3.8 operating env / system]:

python3.7 -m venv ~/venv/py3.7      ## create Python 3.7-based venv
source ~/venv/py3.7/bin/activate    ## activate that venv
deactivate                          ## deactivate that venv (when done, there)

Added to ~/.bashrc:

alias p37='echo "   [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'

Test Python 3.7 venv:

$ p37                                                                                                                 
[Python 3.7 venv (source ~/venv/py3.7/bin/activate)]

(py3.7)$ python --version
Python 3.7.4

(py3.7)$ python
Python 3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53) 
[GCC 9.2.0] 
>>>
霓裳挽歌倾城醉 2024-08-14 05:23:59

假设您当前在 virtualenv 中安装了 python 2.7。但是想要使用python3.2,您必须更新它:

$ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv

然后通过以下方式激活您的virtualenv

$ source activate name_of_your_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:

$ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv

Then activate your virtualenv by:

$ source activate name_of_your_virtualenv

and then do: python --version in shell to check whether your version is now updated.

ぃ弥猫深巷。 2024-08-14 05:23:59

您应该安装该 Python 版本。如果你有的话,基本上

,使用 virtualenv,

virtualenv --python=python3.8 env/place/you/want/to/save/to

使用 venv

python3.8 -m venv env/place/you/want/to/save/to

上面的示例适用于 python3.8,你可以将其更改为具有不同版本的虚拟环境,因为它们安装在你的计算机中。

You should have that Python version installed. If you have it then basically,

With virtualenv,

virtualenv --python=python3.8 env/place/you/want/to/save/to

with venv

python3.8 -m venv env/place/you/want/to/save/to

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.

落花浅忆 2024-08-14 05:23:59

这两个命令应该可以正常工作。

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)

醉梦枕江山 2024-08-14 05:23:59

你可以使用你想要的Python版本来调用virtualenv。例如:

python3 -m virtualenv venv

或者直接指向您的 virtualenv 路径。例如对于Windows:

c:\Python34\Scripts\virtualenv.exe venv

通过运行:

venv/bin/python

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

您可以看到虚拟环境中安装的Python版本

You can call virtualenv with python version you want. For example:

python3 -m virtualenv venv

Or alternatively directly point to your virtualenv path. e.g. for windows:

c:\Python34\Scripts\virtualenv.exe venv

And by running:

venv/bin/python

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

you can see the python version installed in virtual environment

无尽的现实 2024-08-14 05:23:59

-p 方法效果很好,但您必须记住每次都使用它。如果您的目标通常是切换到较新版本的 Python,那么这会很痛苦,而且还可能导致错误。

您的另一个选择是设置一个与 -p 执行相同操作的环境变量。通过您的 ~/.bashrc 文件或您管理登录会话的环境变量的

export VIRTUALENV_PYTHON=/path/to/desired/version

任何位置进行设置:然后 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:

export VIRTUALENV_PYTHON=/path/to/desired/version

Then virtualenv will use that any time you don't specify -p on the command line.

秋凉 2024-08-14 05:23:59

在 Mac 上我使用 pyenv 和 virtualenvwrapper。我必须创建一个新的虚拟环境。你需要自制程序,如果你在 Mac 上,我假设你已经安装了它,但只是为了好玩:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python  {virtual_env_name}

我还首先冻结了我的要求,这样我就可以简单地在新的 virtualenv 中重新安装:

pip install -r requirements.txt

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:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python  {virtual_env_name}

I also froze my requirements first so i could simply reinstall in the new virtualenv with:

pip install -r requirements.txt
街道布景 2024-08-14 05:23:59

2024 年登陆 macOS;使用 pyenv 时:

brew install pyenv

.. 并按照安装后说明设置 pyenv,请参阅; https://github.com/pyenv/pyenv?tab=readme-ov -file#unixmacos

基本上归结为在您的 ~/.bashrc~/.zshrc 文件中添加这些行:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

安装您最喜欢的 python 版本,例如 < code>3.11.6

pyenv install 3.11.6

现在,您可以在项目的同一目录中创建一个新的 python 虚拟环境,如下所示:

~/.pyenv/versions/3.11.6/bin/python -m venv name-of-your-venv

激活环境:

source name-of-your-venv/bin/activate

检查此 python 环境中活动的 python 版本:

(venv) python --version

停用环境:

(venv) deactivate

清理env:

只需删除名称为 python env 的文件夹,因此在这种情况下:

rm -rf name-of-your-venv

不要在 git 中提交您的 virtualenv。为了确保这一点,请将其添加到您的 .gitignore 文件中:

name-of-your-venv/

还要确保该文件夹不用于扫描 linting、测试、覆盖率,因为它会减慢这些进程的速度。因此,在一个 Python 项目中,使用 black 进行 linting,使用 isort 对导入进行排序,使用 pytest 进行单元测试,使用 poetry< /code> 用于管理依赖项,pyproject.toml 文件将包含以下内容:

[tool.pytest.ini_options]
norecursedirs = [".git", "name-of-your-venv"]

[tool.black]
exclude = ".git|name-of-your-venv"

[tool.isort]
skip = [".git", "name-of-your-venv"]

一个好的做法是将您的 virtualenv 命名为 venv.venv

On macOS in 2024; when using pyenv:

brew install 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:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

Install your favorite python version, e.g. 3.11.6

pyenv install 3.11.6

Now, you can create a new python virtual environment, in the same directory of your project, like so:

~/.pyenv/versions/3.11.6/bin/python -m venv name-of-your-venv

Activate the env:

source name-of-your-venv/bin/activate

Check the python version active in this python env:

(venv) python --version

Deactivate the env:

(venv) deactivate

Cleanup the env:

just delete the folder with the name of the python env, so in this case:

rm -rf name-of-your-venv

Do not commit your virtualenv in git. To make sure, add it to your .gitignore file:

name-of-your-venv/

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, and pytest for unit testing, and poetry for managing dependencies, a pyproject.toml file would contain this:

[tool.pytest.ini_options]
norecursedirs = [".git", "name-of-your-venv"]

[tool.black]
exclude = ".git|name-of-your-venv"

[tool.isort]
skip = [".git", "name-of-your-venv"]

A good practise is to name your virualenv venv or .venv

ヤ经典坏疍 2024-08-14 05:23:59

更简单的是,使用命令替换为您找到 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>

动次打次papapa 2024-08-14 05:23:59

在 Windows 上:

py -3.4x32 -m venv venv34

或者

py -2.6.2 -m venv venv26

这使用 py 启动器 这将为您找到正确的 python 可执行文件(假设您已经安装了它)。

On windows:

py -3.4x32 -m venv venv34

or

py -2.6.2 -m venv venv26

This uses the py launcher which will find the right python executable for you (assuming you have it installed).

懒的傷心 2024-08-14 05:23:59

对于 Windows 来说,这些似乎有点过于复杂。如果您在运行 python 3.3 或更高版本的 Windows 上,则可以使用 python 启动器 py 更轻松地完成此操作。只需安装不同的 python 版本,然后运行:

py -[my version] -m venv env

这将使用 python [my version] 在当前目录中创建一个名为 env 的虚拟环境。举个例子:

py -3.7 -m venv env
./env/Scripts/activate

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

py -[my version] -m venv env

This will create a virtual environment called env in your current directory, using python [my version]. As an example:

py -3.7 -m venv env
./env/Scripts/activate

This creates a virtual environment called env using python3.7 and activates it. No paths or other complex stuff required.

咿呀咿呀哟 2024-08-14 05:23:59

正如多个答案中已经提到的,使用 virtualenv 是一个干净的解决方案。然而,每个人都应该意识到的一个小陷阱是,如果在 bash_aliases 中设置了 python 的别名,例如:

python=python3.6

该别名也将在虚拟环境中使用。因此,在这种情况下,无论使用什么解释器来创建环境,在虚拟环境中运行 python -V 将始终输出 3.6

virtualenv venv --python=pythonX.X

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:

python=python3.6

this alias will also be used inside the virtual environment. So in this scenario running python -V inside the virtual env will always output 3.6 regardless of what interpreter is used to create the environment:

virtualenv venv --python=pythonX.X
池木 2024-08-14 05:23:59

对于 Mac(High Sierra),在 python3 上安装 virtualenv 并为 python2 创建 virtualenv:

 $ python3 -m pip install virtualenv
 $ python3 -m virtualenv --python=python2 vp27
 $ source vp27/bin/activate
 (vp27)$ python --version
 Python 2.7.14

For Mac(High Sierra), install the virtualenv on python3 and create a virtualenv for python2:

 $ python3 -m pip install virtualenv
 $ python3 -m virtualenv --python=python2 vp27
 $ source vp27/bin/activate
 (vp27)$ python --version
 Python 2.7.14
挽袖吟 2024-08-14 05:23:59

我在Windows上使用了这个答案

https://stackoverflow.com/a/22793687/15435022

py -3.4 -m venv c:\path\to\wherever\you\want\it

I utilized this answer for Windows

https://stackoverflow.com/a/22793687/15435022

py -3.4 -m venv c:\path\to\wherever\you\want\it
沐歌 2024-08-14 05:23:59

我使用 pyenv 来管理我的 python 版本。

pyenv install 3.7.3
pyenv local 3.7.3

检查您的 python 版本:

$ python --version
Python 3.7.3

使用 venv 创建虚拟环境:

python -m venv .

然后激活虚拟环境:

source bin/activate

检查你的python版本:

$ python --version
Python 3.7.3

你可能需要删除之前的虚拟环境

rm -rf bin

I use pyenv to manage my python version.

pyenv install 3.7.3
pyenv local 3.7.3

Check your python version:

$ python --version
Python 3.7.3

Create the virtual environment with venv:

python -m venv .

Then activate the Virtual Environment:

source bin/activate

Check your python version:

$ python --version
Python 3.7.3

You may need to remove the previous virtual environment

rm -rf bin
孤单情人 2024-08-14 05:23:59

在 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

cd /usr/bin/Python-3.7.8/

如果需要,请检查内容查看到目前为止您所拥有的内容: ls

sudo time ./configure
sudo time make
time sudo make install
time make clean

检查您的 python 设置和报告方式:

which python
python --version

应该全部与您的主要安装相关(对我来说是 Python 3.9.5)

要检查您的新安装:

which python 3.7
python3.7 --version

应该是所有与您的 3.7.8 安装相关的内容

如果您想运行它进行检查,请执行以下操作:

python3.7
exit()

安装 venv:

sudo apt install venv

要创建 venv(可能在您的存储库中,如果是,请将 .venv 添加到 .gitignore):

python3.7 -m venv .venv

要激活您的 venv:

source .venv/bin/activate

检查您的 venv:版本:

python --version

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

cd /usr/bin/Python-3.7.8/

Check the contents if you wanted to see what you have so far: ls

sudo time ./configure
sudo time make
time sudo make install
time make clean

Check how your python is set up and reporting:

which python
python --version

Should be all relating to your primary install (Python 3.9.5 for me)

To check your new install:

which python 3.7
python3.7 --version

Should be all relating to your 3.7.8 install

If you want to run it to check, do:

python3.7
exit()

Install venv:

sudo apt install venv

To create a venv (maybe in your repo, if so, add .venv to .gitignore):

python3.7 -m venv .venv

To activate your venv:

source .venv/bin/activate

Check your version:

python --version
星軌x 2024-08-14 05:23:59

在Linux的Windows子系统中:

  1. 为python3创建环境:

    virtualenv --python=/usr/bin/python3 env
    
  2. 激活它:

    源环境/bin/activate
    

In windows subsystem for linux:

  1. Create environment for python3:

    virtualenv --python=/usr/bin/python3 env
    
  2. Activate it:

    source env/bin/activate
    
寂寞清仓 2024-08-14 05:23:59

2020 年底:

使用 virtualenv 最无缝的体验(附加好处:使用任何可能的 python 版本)将是使用 pyenv 及其(捆绑)pyenv-virtualenv 插件(参见 https://realpython.com/intro-to-pyenv/#virtual-environments-and-pyenv

用法: pyenv virtualenv;

安装:

cf https://github.com/ pyenv/pyenv-installer

话虽这么说,现在代替使用 virtualenv (和 pip) 的最佳替代方案是 Poetry (与上面指出的 pyenv 一起处理不同的 python 版本)。

另一种选择,因为它由 PyPA(pip 和 PyPI 背后的组织)直接支持,并且自 5 月底以来已重新发布(自 2018 年末以来一直没有发布......)将是 Pipenv

End 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:

cf https://github.com/pyenv/pyenv-installer

That being said, nowadays the best possible alternative instead of using virtualenv (and pip) would be Poetry (along with pyenv 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

荆棘i 2024-08-14 05:23:59

这适用于我在 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

混吃等死 2024-08-14 05:23:59

这个问题的答案不应该那么复杂...

TL,DR:

在你的系统上安装你喜欢的Python版本并使用:

/c/path/to/any/version/of/python -m venv my_venv

=================== =========================

我使用 venv 来安装虚拟环境,

python -m venv <where/to/and/name_of_venv>

如果你尝试 which python 你会看到 which python当你说“python”时,你指的是。例如,对我来说是:

which python

结果:
/c/Program Files/Python36/python

所以,现在你有了答案!
您可以在系统上安装任何版本的 python,并同时安装多个版本。因此,例如我在这个目录中安装了Python3.7:“C:\Program Files\Python37”。
因此,现在我不再使用“python”,而是通过 /c/Program\ Files/Python37/python 指定哪个 python:(

 /c/Program\ Files/Python37/python -m venv my_venv

不要忘记转义路径中的空格)

就是这样!

Answer to this question shouldn't be that complicated...

TL,DR:

install as many versions of python you prefer on your system and use:

/c/path/to/any/version/of/python -m venv my_venv

============================================

I use venv to install virtual environments with

python -m venv <where/to/and/name_of_venv>

if you try which python you will see which python you are referring to, when saying "python". for example, for me it is:

which python

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:

 /c/Program\ Files/Python37/python -m venv my_venv

(don't forget to escape the space in the path)

That's it!

秋意浓 2024-08-14 05:23:59

,上述答案是正确的,并且在基于 Unix 的系统(例如 Linux 和 Linux)上运行良好。 MAC OS X

我尝试为 Python2 创建 virtualenvPython3 使用以下命令。

这里我使用了 venv2 & venv3 作为 Python2Python2 的名称。分别是Python3

Python2 »

MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
Running virtualenv with interpreter /usr/local/bin/python2
New python executable in /Users/admin/venv2/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ ls venv2/bin/
activate        easy_install        pip2.7          python2.7
activate.csh        easy_install-2.7    python          wheel
activate.fish       pip         python-config
activate_this.py    pip2            python2
MacBook-Pro-2:~ admin$ 

Python3 »

MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/venv3/bin/python3
Also creating executable in /Users/admin/venv3/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ ls venv3/bin/
activate        easy_install        pip3.6          python3.6
activate.csh        easy_install-3.6    python          wheel
activate.fish       pip         python-config
activate_this.py    pip3            python3
MacBook-Pro-2:~ admin$ 

检查Python安装位置

MacBook-Pro-2:~ admin$ which python2
/usr/local/bin/python2
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ which python3
/usr/local/bin/python3
MacBook-Pro-2:~ admin$ 

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.

Python2 »

MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
Running virtualenv with interpreter /usr/local/bin/python2
New python executable in /Users/admin/venv2/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ ls venv2/bin/
activate        easy_install        pip2.7          python2.7
activate.csh        easy_install-2.7    python          wheel
activate.fish       pip         python-config
activate_this.py    pip2            python2
MacBook-Pro-2:~ admin$ 

Python3 »

MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/venv3/bin/python3
Also creating executable in /Users/admin/venv3/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ ls venv3/bin/
activate        easy_install        pip3.6          python3.6
activate.csh        easy_install-3.6    python          wheel
activate.fish       pip         python-config
activate_this.py    pip3            python3
MacBook-Pro-2:~ admin$ 

Checking Python installation locations

MacBook-Pro-2:~ admin$ which python2
/usr/local/bin/python2
MacBook-Pro-2:~ admin$ 
MacBook-Pro-2:~ admin$ which python3
/usr/local/bin/python3
MacBook-Pro-2:~ admin$ 
怼怹恏 2024-08-14 05:23:59

我使用 Windows,所以我应该在 pthon 路径上使用 .exe

virtualenv -p=C:\Python27\python2.exe <envname>

I use Windows so I should use .exe on the pthon path

virtualenv -p=C:\Python27\python2.exe <envname>
栩栩如生 2024-08-14 05:23:59

假设我想使用 python 3.8 并且我正在使用 MacOS。

brew install [email protected]

然后,

python3.8 -m venv venv

Suppose I want to use python 3.8 and I'm using MacOS.

brew install [email protected]

Then,

python3.8 -m venv venv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文