python 配置虚拟环境,多版本管理

在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.x。所有第三方的包都会被pip安装到Python3的site-packages目录下。
如果我们要同时开发多个应用程序,那这些应用程序都会共用一个Python,就是安装在系统的Python 3。如果应用A需要python 2.7,而应用B需要python 3怎么办?
这种情况下,每个应用可能需要各自拥有一套“独立”的Python运行环境。virtualenv就是用来为一个应用创建一套“隔离”的Python运行环境。

一、virtualenvwrapper

virtualenvwrapper是virtualenv的扩展包,用于更方便管理虚拟环境,可以:

  • 将所有虚拟环境整合在一个目录下
  • 统一管理虚拟环境
  • 快速切换虚拟环境

1、安装

windows-pip安装:

C:\Users\Rnanprince>pip install virtualenvwrapper-win
Collecting virtualenvwrapper-win
  Downloading virtualenvwrapper-win-1.2.6.tar.gz (21 kB)
Collecting virtualenv
  Using cached virtualenv-20.0.25-py2.py3-none-any.whl (4.7 MB)
...
Successfully built virtualenvwrapper-win
Installing collected packages: virtualenv, virtualenvwrapper-win
Successfully installed virtualenv-20.0.25 virtualenvwrapper-win-1.2.6

Linux-apt和pip安装

apt安装:
$ sudo apt-get install virtualenvwrapper
pip安装:
$ sudo pip install virtualenvwrapper

$ sudo echo "source virtualenvwrapper.sh">>~/.bashrc

2、查看版本

C:\Users\Rnanprince>virtualenv --version
virtualenv 20.0.25 from d:\program files\anaconda3\lib\site-packages\virtualenv\__init__.py
ERROR:root:SystemExit: 0

3、创建运行环境

 C:\Users\Rnanprince>mkvirtualenv --python=python3 venv3
created virtual environment CPython3.7.3.final.0-64 in 4898ms
  creator CPython3Windows(dest=C:\Users\Rnanprince\Envs\venv3, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Rnanprince\AppData\Local\pypa\virtualenv)
    added seed packages: pip==20.1.1, setuptools==47.3.1, wheel==0.34.2
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

4、列出虚拟环境列表

(venv) C:\Users\Rnanprince>lsvirtualenv
或
(venv) C:\Users\Rnanprince>workon
Pass a name to activate one of the following virtualenvs:
==============================================================================
venv2
venv3

5、激活 - 切换环境

(venv3) C:\Users\Rnanprince>workon venv2
(venv2) C:\Users\Rnanprince>

6、退出环境

(venv2) C:\Users\Rnanprince>deactivate
C:\Users\Rnanprince>

7、删除环境

(venv3) C:\Users\Rnanprince>rmvirtualenv venv
    Deleted C:\Users\Rnanprince\Envs\venv

8、其余参数:

  •    add2virtualenv:    add directory to the import path
  •    cdproject:    change directory to the active project
  •    cdsitepackages:    change to the site-packages directory
  •    cdvirtualenv:    change to the $VIRTUAL_ENV directory
  •    lssitepackages:    list contents of the site-packages directory
  •    lsvirtualenv:    list virtualenvs
  •    mkproject:    create a new project directory and its associated virtualenv
  •    mkvirtualenv:    Create a new virtualenv in $WORKON_HOME
  •    rmvirtualenv:    Remove a virtualenv
  •    setprojectdir:    associate a project directory with a virtualenv
  •    toggleglobalsitepackages:    turn access to global site-packages on/off
  •    virtualenvwrapper:    show this help message
  •    whereis:    return full path to executable on path.
  •    workon:    list or change working virtualenvs

二、使用conda管理

conda可以直接创建不同python版本的虚拟环境。virtualenv只是指定闯将不同的python版本的虚拟环境,前提是你已经安装了不同版本的python,与conda相比没有conda灵活。

1、安装

下载anaconda的python可以直接使用conda工具。

查看conda版本:

C:\Users\Rnanprince>conda --version
conda 4.8.3

 更新conda版本:

C:\Users\Rnanprince>pip install --upgrade conda

2、创建虚拟环境

创建不同的python版本,直接写出版本号就好了,还可以同时安装想要的库。

C:\Users\Rnanprince>conda create -n venv2 python=2.7
C:\Users\Rnanprince>conda create -n venv2 python=3.6
The following NEW packages will be INSTALLED:

  ca-certificates    pkgs/main/win-64::ca-certificates-2020.1.1-0
  certifi            pkgs/main/win-64::certifi-2020.6.20-py37_0
  openssl            pkgs/main/win-64::openssl-1.1.1g-he774522_0
  pip                pkgs/main/win-64::pip-20.1.1-py37_1
  python             pkgs/main/win-64::python-3.7.7-h81c818b_4
  setuptools         pkgs/main/win-64::setuptools-47.3.1-py37_0
  sqlite             pkgs/main/win-64::sqlite-3.32.3-h2a8f88b_0
  vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_2
  wheel              pkgs/main/win-64::wheel-0.34.2-py37_0
  wincertstore       pkgs/main/win-64::wincertstore-0.2-py37_0
  zlib               pkgs/main/win-64::zlib-1.2.11-h62dcd97_4

3、列出虚拟环境

C:\Users\Rnanprince>conda info -e
或:
C:\Users\Rnanprince>conda env list
# conda environments:
#
base                  *  D:\Program Files\Anaconda3
venv2                    D:\Program Files\Anaconda3\envs\venv2
venv3                    D:\Program Files\Anaconda3\envs\venv3

4、激活虚拟环境

windows:
C:\Users\Rnanprince>activate venv2

Linux:
source activate venv2

5、退出虚拟环境

windows:
(venv2) C:\Users\Rnanprince>conda deactivate
Linux:
source conda deactivate

6、删除虚拟环境

C:\Users\Rnanprince>conda remove --name venv --all

7、查看安装包

当前环境:

(venv2) C:\Users\Rnanprince>conda list
# packages in environment at D:\Program Files\Anaconda3\envs\venv2:
#
# Name                    Version                   Build  Channel
ca-certificates           2020.1.1                      0
certifi                   2019.11.28               py27_0
pip                       19.3.1                   py27_0
python                    2.7.18               hfb89ab9_0
setuptools                44.0.0                   py27_0
sqlite                    3.30.1               h0c8e037_0
vc                        9                    h7299396_1
vs2008_runtime            9.00.30729.1         hfaea7d5_1
wheel                     0.33.6                   py27_0
wincertstore              0.2              py27hf04cefb_0

某个指定环境:

C:\Users\Rnanprince>conda list -n venv2
# packages in environment at D:\Program Files\Anaconda3\envs\venv2:
#
# Name                    Version                   Build  Channel
ca-certificates           2020.1.1                      0
certifi                   2019.11.28               py27_0
pip                       19.3.1                   py27_0
python                    2.7.18               hfb89ab9_0
setuptools                44.0.0                   py27_0
sqlite                    3.30.1               h0c8e037_0
vc                        9                    h7299396_1
vs2008_runtime            9.00.30729.1         hfaea7d5_1
wheel                     0.33.6                   py27_0
wincertstore              0.2              py27hf04cefb_0

8、安装包处理

安装:

C:\Users\Rnanprince>conda install -n venv2 numpy

C:\Users\Rnanprince>conda list -n venv2
C:\Users\Rnanprince>conda list -n venv2
# packages in environment at D:\Program Files\Anaconda3\envs\venv2:
#
# Name                    Version                   Build  Channel
blas                      1.0                         mkl
ca-certificates           2020.1.1                      0
certifi                   2019.11.28               py27_0
icc_rt                    2019.0.0             h0cc432a_1
intel-openmp              2020.1                      216
mkl                       2020.1                      216
mkl-service               2.3.0            py27h0b88c2a_0
mkl_fft                   1.0.15           py27h44c1dab_0
numpy                     1.16.6           py27hcd21bde_0
numpy-base                1.16.6           py27hb1d0314_0
pip                       19.3.1                   py27_0
python                    2.7.18               hfb89ab9_0
setuptools                44.0.0                   py27_0
six                       1.15.0                     py_0
sqlite                    3.30.1               h0c8e037_0
vc                        9                    h7299396_1
vs2008_runtime            9.00.30729.1         hfaea7d5_1
wheel                     0.33.6                   py27_0
wincertstore              0.2              py27hf04cefb_0

更新:

C:\Users\Rnanprince>conda update -n venv2 numpy

删除:

C:\Users\Rnanprince>conda remove -n venv2 numpy

C:\Users\Rnanprince>conda list -n venv2
# packages in environment at D:\Program Files\Anaconda3\envs\venv2:
#
# Name                    Version                   Build  Channel
ca-certificates           2020.1.1                      0
certifi                   2019.11.28               py27_0
pip                       19.3.1                   py27_0
python                    2.7.18               hfb89ab9_0
setuptools                44.0.0                   py27_0
sqlite                    3.30.1               h0c8e037_0
vc                        9                    h7299396_1
vs2008_runtime            9.00.30729.1         hfaea7d5_1
wheel                     0.33.6                   py27_0
wincertstore              0.2              py27hf04cefb_0

三、Linux系统切换系统python版本

1、原系统各python对应软连接

ll /usr/bin/ |grep python

2、保存下原有python3.6的软连接

3、将默认python设置为python2.7

ln -s /usr/bin/python2.7 /usr/bin/python

4、改回默认为3.6就重复以上三步

你可能感兴趣的:(python,工具,python,anaconda,virtualenv)