Files
mkdocs/docs/macos/python/virtuel.md
2019-03-15 20:20:37 +01:00

3.9 KiB

Environnement virtuel en Python (> 3.3)

On crée un dossier pour regrouper tous les environnements virtuels:

$ cd
$ mkdir venv
$ cd venv

On crée l'environnement virtuel 'tuto-virtuel-env':

$ python -m venv tuto-virtuel-env

$ cd tuto-virtuel-env/
total 8
drwxr-xr-x   6 bruno  staff  192 15 mar 07:29 .
drwxr-xr-x   3 bruno  staff   96 15 mar 07:29 ..
drwxr-xr-x  12 bruno  staff  384 15 mar 07:29 bin
drwxr-xr-x   2 bruno  staff   64 15 mar 07:29 include
drwxr-xr-x   3 bruno  staff   96 15 mar 07:29 lib
-rw-r--r--   1 bruno  staff  105 15 mar 07:29 pyvenv.cfg

L'environnement est crée avec la version courante de Python.

Si plusieurs versions de Python sont installées sur le système, on peut spécifier une version particulière en exécutant python3.x:

$ python3.6 -m venv tuto-venv-py36

On peut créer un environnement sans pip:

$ python -m venv env-sans-pip --without-pip

$ source env-sans-pip/bin/activate
(env-sans-pip) bruno@SilverBook:~/venv$ pip
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 583, in _build_master

On l'active:

# Pour bash:
$ source tuto-virtuel-env/bin/activate
(tuto-virtuel-env) bruno@SilverBook:~/venv$

# Pour csh ou fish:
activate.csh
activate.fish

On lance Python:

(tuto-virtuel-env) bruno@SilverBook:~/venv$ python
Python 3.7.2 (default, Feb 12 2019, 08:15:36)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python3.7/site-packages', '/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Users/bruno/venv/tuto-virtuel-env/lib/python3.7/site-packages']

Sortir de l'environnement virtuel:

(tuto-virtuel-env) bruno@SilverBook:~/venv$ deactivate
bruno@SilverBook:~/venv$

Contenu de l'environnement virtuel:

(tuto-virtuel-env) bruno@SilverBook:~/venv/tuto-virtuel-env$ cd bin/
total 64
drwxr-xr-x  12 bruno  staff   384 15 mar 07:29 .
drwxr-xr-x   6 bruno  staff   192 15 mar 07:29 ..
-rw-r--r--   1 bruno  staff  2234 15 mar 07:29 activate
-rw-r--r--   1 bruno  staff  1290 15 mar 07:29 activate.csh
-rw-r--r--   1 bruno  staff  2442 15 mar 07:29 activate.fish
-rwxr-xr-x   1 bruno  staff   263 15 mar 07:29 easy_install
-rwxr-xr-x   1 bruno  staff   263 15 mar 07:29 easy_install-3.7
-rwxr-xr-x   1 bruno  staff   245 15 mar 07:29 pip
-rwxr-xr-x   1 bruno  staff   245 15 mar 07:29 pip3
-rwxr-xr-x   1 bruno  staff   245 15 mar 07:29 pip3.7
lrwxr-xr-x   1 bruno  staff    51 15 mar 07:29 python -> /usr/local/Cellar/python/3.7.2_2/libexec/bin/python
lrwxr-xr-x   1 bruno  staff     6 15 mar 07:29 python3 -> python

(tuto-virtuel-env) bruno@SilverBook:~/venv/tuto-virtuel-env/bin$ which pip3
/Users/bruno/venv/tuto-virtuel-env/bin/pip3
(tuto-virtuel-env) bruno@SilverBook:~/venv/tuto-virtuel-env/bin$ which python3
/Users/bruno/venv/tuto-virtuel-env/bin/python3

Installer un module avec pip:

(tuto-virtuel-env) bruno@SilverBook:~/venv/tuto-virtuel-env/bin$ pip3 install easy-parse
(tuto-virtuel-env) bruno@SilverBook:~/venv/tuto-virtuel-env/bin$ pip3 freeze
...
docutils==0.14
easy-parse==0.1.1
entrypoints==0.3
...
(tuto-virtuel-env) bruno@SilverBook:~/venv/tuto-virtuel-env/bin$ deactivate

bruno@SilverBook:~/venv/tuto-virtuel-env/bin$ pip3 freeze
...
docutils==0.14
entrypoints==0.3
...

Le module installé dans l'environnement virtuel est disponible UNIQUEMENT dans l'environnement virtuel.

https://www.python.org/dev/peps/pep-0405/

https://docs.python.org/fr/3/library/venv.html