python configuration virtual environment, multi version management

Keywords: Python pip SQLite Windows

When developing Python applications, the system has only one version of Python 3 installed: 3.x. All third-party packages will be installed by pip in the site packages directory of Python 3.
If we want to develop multiple applications at the same time, these applications will share the same python, which is Python 3 installed on the system. What if application A needs Python 2.7 and application B needs Python 3?
In this case, each application may need to have its own "independent" Python running environment. virtualenv is used to create a set of "isolated" Python running environment for an application.

1, virtualenvwrapper

virtualenvwrapper is an extension package of virtualenv, which is used for more convenient management of virtual environment. You can:

  • Consolidate all virtual environments in one directory
  • Unified management of virtual environment
  • Fast switch virtual environment

1. Installation

Windows PIP installation:

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 and pip installation

apt Installation:
$ sudo apt-get install virtualenvwrapper
pip Installation:
$ sudo pip install virtualenvwrapper

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

2. View version

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. Create a running environment

 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. List virtual environments

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

5. Activate - switch environment

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

6. Exit environment

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

7. Delete environment

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

8. Other parameters:

  •    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

2, Using conda management

conda can directly create virtual environments of different Python versions. virtualenv only specifies the virtual environment of different versions of python, provided that you have installed different versions of python, which is not flexible compared with conda.

1. Installation

Download the python of anaconda and use the conda tool directly.

To view the conda version:

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

Update conda version:

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

2. Create a virtual environment

Create different python versions, write out the version number directly, and install the desired libraries at the same time.

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. List virtual environments

C:\Users\Rnanprince>conda info -e
//Or:
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. Activate virtual environment

windows:
C:\Users\Rnanprince>activate venv2

Linux:
source activate venv2

 

5. Exit virtual environment

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

6. Delete virtual environment

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

7. View installation package

Current environment:

(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

A specified environment:

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. Installation package processing

Installation:

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

C:\Users\Rnanprince>conda list -n venv2

to update:

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

Delete:

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

Posted by dross613 on Sat, 27 Jun 2020 01:13:39 -0700