Table des matières

Un environnement virtuel est un outil pour garder les dépendances requises par différents projets dans des emplacements séparés, en créant des environnements virtuels Python pour eux. Il résout le dilemme “le projet X dépend de la version 1.x mais le projet Y nécessite la 4.x”, et garde votre répertoire site-packages global propre et gérable.

Par exemple, vous pouvez travailler sur un projet qui nécessite Django 1.3 tout en maintenant aussi un projet qui nécessite Django 1.0.

virtualenv est un outil pour créer des environnements virtuels Python isolés. virtualenv crée un dossier qui contient tous les exécutables nécessaires pour utiliser les paquets qu’un projet Python pourrait nécessiter.

Pré-requis

Python 2 ou Python 3.

Installation de python

Python 2 est installé par défaut sur nombre de distribution.

Pour Python 3, il est parfois nécessaire de passer par un repository supplémentaire.

Installation de Python 2 sur Centos/RHEL/OL 7 :

[root@sandbox ~]# yum install python python-virtualenv

Installation de Python 3 sur Centos/RHEL/OL 7 :

Pour python 3, il faut d'abord passer par l'installation des “software collection”.

[root@sandbox ~]# yum install centos-release-scl

Puis on installe Python 3.6.

[root@sandbox ~]# yum install rh-python36

Pour pouvoir utiliser Python 3.6, il faut d’abord l’activer, pour cela :

[root@sandbox ~]# scl enable rh-python36 bash

Cette commande ne fait qu’appeler le script /opt/rh/rh-python36/enable qui change les variables d’environnement du Shell.

Vous pouvez vérifier la version de Python avec :

[root@sandbox ~]# python --version

La version est correct, les utilisateurs peuvent désormais créer des virtualenv.

Création de l’environnement

Attention:

Pensez à activer Python 3 si ce n'est déjà le python par défaut.

scl enable rh-python36 bash

Création du dossier de votre projet :

[user01@sandbox ~]$ mkdir myproject
[user01@sandbox ~]$ cd myproject
[user01@sandbox myproject]$ 

Création du virtualenv avec Python 3 :

[user01@sandbox myproject]$ python3 -m venv venv
[user01@sandbox myproject]$ 

Création du virtualenv avec Python 2 :

[user01@sandbox myproject]$ python2 -m virtualenv venv
New python executable in /home/user01/myproject/venv/bin/python2
Also creating executable in /home/user01/myproject/venv/bin/python
Installing setuptools, pip, wheel...done.
[user01@sandbox myproject]$ 

Avant de commencer a travailler sur votre projet, activer l’environnement correspondant :

[user01@sandbox myproject]$ . venv/bin/activate
(venv) [user01@sandbox myproject]$ 

Comme vous pouvez le constater, le prompt a changé vous indiquant que vous vous trouvez désormais dans un Virtualenv.

Utilisation de l'environnement

Un exemple d'utilisation courant est l'installation d'un module pip, botocore par exemple.

(venv) [user01@sandbox myproject]$ pip install botocore
Collecting botocore
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/70/f2/9e591425a2543a270c7e30551472f425fd31ddc8b117f6cfa6f85c98a068/botocore-1.12.202-py2.py3-none-any.whl (5.6MB)
    100% |████████████████████████████████| 5.6MB 220kB/s 
Collecting urllib3<1.26,>=1.20; python_version >= "3.4" (from botocore)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/e6/60/247f23a7121ae632d62811ba7f273d0e58972d75e58a94d329d51550a47d/urllib3-1.25.3-py2.py3-none-any.whl (150kB)
    100% |████████████████████████████████| 153kB 2.8MB/s 
Collecting jmespath<1.0.0,>=0.7.1 (from botocore)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/83/94/7179c3832a6d45b266ddb2aac329e101367fbdb11f425f13771d27f225bb/jmespath-0.9.4-py2.py3-none-any.whl
Collecting docutils<0.15,>=0.10 (from botocore)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/36/fa/08e9e6e0e3cbd1d362c3bbee8d01d0aedb2155c4ac112b19ef3cae8eed8d/docutils-0.14-py3-none-any.whl (543kB)
    100% |████████████████████████████████| 552kB 840kB/s 
Collecting python-dateutil<3.0.0,>=2.1; python_version >= "2.7" (from botocore)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/41/17/c62faccbfbd163c7f57f3844689e3a78bae1f403648a6afb1d0866d87fbb/python_dateutil-2.8.0-py2.py3-none-any.whl (226kB)
    100% |████████████████████████████████| 235kB 1.1MB/s 
Collecting six>=1.5 (from python-dateutil<3.0.0,>=2.1; python_version >= "2.7"->botocore)
  Cache entry deserialization failed, entry ignored
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Installing collected packages: urllib3, jmespath, docutils, six, python-dateutil, botocore
Successfully installed botocore-1.12.202 docutils-0.14 jmespath-0.9.4 python-dateutil-2.8.0 six-1.12.0 urllib3-1.25.3
You are using pip version 9.0.1, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv) [user01@sandbox myproject]$ 

Mise à jour de l'environnement

La mise à jour de l'environnement consiste la plupart du temps à mettre à jour “pip” et les modules, pour cela faites :

(venv) [user01@sandbox myproject]$ pip install --upgrade pip
Cache entry deserialization failed, entry ignored
Collecting pip
  Using cached https://files.pythonhosted.org/packages/62/ca/94d32a6516ed197a491d17d46595ce58a83cbb2fca280414e57cd86b84dc/pip-19.2.1-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-19.2.1
(venv) [user01@sandbox myproject]$ 

Destruction de l'environnement

La destruction d'un environnement ne consiste en fait qu'à supprimer le dossier de votre projet.

Attention:

Attention, pensez a quitter le virtualenv avant de supprimer son dossier !

(venv) [user01@sandbox myproject]$ exit
exit
[user01@sandbox myproject]$ cd ..
[user01@sandbox ~]$ rm -Rf myproject/
[user01@sandbox ~]$