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


当前回答

当您在共享托管环境中,需要从源代码安装和编译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   

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

其他回答

更简单的是,使用命令替换为您查找python2:

virtualenv-p$(哪个python2)<path/to/new/virtalenv/>

或者使用virtualvwrapper时:

mkvirtualenv-p$(哪个python2)<env_name>

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

我尝试使用以下命令为Python2&Python3创建virtualenv。

这里我分别使用了venv2和venv3作为Python2和Python3的名称。

蟒蛇2»

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$ 

蟒蛇3»

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$ 

在mac上,我使用pyenv和virtualnvwrapper。我必须创造一个新的虚拟世界。你需要自制程序,如果你在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

〔2019年11月〕我需要在基于Python 3.8的Arch Linux系统上安装Python 3.7环境(env)。Python 3.7已不在系统中,因此我无法降级Python,以安装所需的包。

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


下载Python版本源文件:

我从

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

to

/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.html12.2.创建虚拟环境用于创建和管理虚拟环境的模块称为venv。venv通常会安装您可用的最新版本的Python。如果您的系统上有多个版本的Python,则可以通过运行python3或您想要的任何版本来选择特定的Python版本。要创建虚拟环境,请确定要放置虚拟环境的目录,并以脚本的形式运行venv模块,路径如下:python3-m venv教程env这将创建教程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] 
>>>

假设您当前在virtualenv中安装了python 2.7。但要使用python3.2,您必须使用以下内容进行更新:

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

然后通过以下方式激活virtualenv:

$ source activate name_of_your_virtualenv

然后执行:python——shell中的版本,以检查您的版本现在是否已更新。