291 lines
7.5 KiB
Markdown
291 lines
7.5 KiB
Markdown
# Environnement virtuel en Python (> 3.3)
|
||
|
||
|
||
|
||
### venv
|
||
|
||
On crée un dossier pour regrouper tous les environnements virtuels:
|
||
|
||
```bash
|
||
$ cd
|
||
$ mkdir venv
|
||
$ cd venv
|
||
```
|
||
|
||
|
||
|
||
On crée l'environnement virtuel 'tuto-virtuel-env':
|
||
|
||
```bash
|
||
$ 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`:
|
||
|
||
```bash
|
||
$ python3.6 -m venv tuto-venv-py36
|
||
```
|
||
|
||
On peut créer un environnement sans `pip`:
|
||
|
||
```bash
|
||
$ 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:
|
||
|
||
```bash
|
||
# 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:
|
||
|
||
```bash
|
||
(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:
|
||
|
||
```bash
|
||
(tuto-virtuel-env) bruno@SilverBook:~/venv$ deactivate
|
||
bruno@SilverBook:~/venv$
|
||
```
|
||
|
||
|
||
|
||
Contenu de l'environnement virtuel:
|
||
|
||
```bash
|
||
(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
|
||
```
|
||
|
||
|
||
|
||
Mettre-à-jour `pip`, `setuptools` et installer `wheel`:
|
||
|
||
```bash
|
||
(tuto-virtuel-env) bruno@SilverBook:~/venv/tuto-virtuel-env/bin$ pip3 install -U pip setuptools wheel
|
||
```
|
||
|
||
|
||
|
||
Installer un module avec `pip`:
|
||
|
||
```bash
|
||
(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://www.python.org/dev/peps/pep-0405/)
|
||
|
||
[https://docs.python.org/fr/3/library/venv.html](https://docs.python.org/fr/3/library/venv.html)
|
||
|
||
|
||
|
||
#### Virtual environnment courant:
|
||
|
||
```python
|
||
❯ python
|
||
Python 3.9.6 (default, Jun 28 2021, 19:24:41)
|
||
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
|
||
Type "help", "copyright", "credits" or "license" for more information.
|
||
>>> import sys
|
||
>>> print(sys.prefix)
|
||
/Users/bruno/Documents/venv/soco-cli
|
||
>>> print(sys.executable)
|
||
/Users/bruno/Documents/venv/soco-cli/bin/python
|
||
>>>
|
||
```
|
||
|
||
```bash
|
||
❯ which -a python
|
||
/Users/bruno/Documents/venv/soco-cli/bin/python
|
||
/usr/bin/python
|
||
```
|
||
|
||
|
||
|
||
### pyvenv
|
||
|
||
#### Installer des modules pour un projet:
|
||
|
||
```bash
|
||
cd myproject
|
||
pipenv install requests
|
||
```
|
||
|
||
|
||
|
||
### Mise-à-jour de Python dans un venv
|
||
|
||
1. On sauvegarde les dépendances:
|
||
|
||
```bash
|
||
~/Documents/venv mkdocs soco-cli
|
||
|
||
❯ source soco-cli/bin/activate
|
||
soco-cli ❯ pip3 freeze > requirements.txt
|
||
soco-cli ❯ deactivate
|
||
```
|
||
|
||
2. On supprime le venv:
|
||
|
||
```bash
|
||
❯ rm -rf soco-cli
|
||
```
|
||
|
||
3. On recrée le venv (avec la version courante de python):
|
||
|
||
```bash
|
||
# avec la version courante de python:
|
||
❯ python3 -m venv soco-cli
|
||
|
||
# avec une version précise de python:
|
||
❯ python3.10 -m venv soco-cli
|
||
```
|
||
|
||
4. On met à jour les outils:
|
||
|
||
```bash
|
||
❯ source soco-cli/bin/activate
|
||
soco-cli ❯ pip3 install -U pip setuptools wheel
|
||
```
|
||
|
||
5. On réinstalle les dépendances:
|
||
|
||
```bash
|
||
soco-cli ❯ pip3 install -r requirements.txt
|
||
soco-cli ❯ deactivate
|
||
```
|
||
|
||
```
|
||
❯ $HOME/Documents/venv/soco-cli/bin/soco -v
|
||
soco-cli version: 0.4.52
|
||
soco version: 0.28.0
|
||
python version: 3.10.8
|
||
command path:
|
||
```
|
||
|
||
|
||
|
||
### Installer LiveboxMonitor dans un venv
|
||
|
||
Cloner le [dépot](https://p-dor.github.io/LiveboxMonitor/):
|
||
|
||
```bash
|
||
~/Downloads $ git clone https://github.com/p-dor/LiveboxMonitor.git
|
||
```
|
||
|
||
Créer le venv:
|
||
|
||
```bash
|
||
~/Documents/venv $ python -m venv liveboxmonitor
|
||
```
|
||
|
||
Activer le venv:
|
||
|
||
```bash
|
||
~/Documents/venv $ source liveboxmonitor/bin/activate
|
||
```
|
||
|
||
Mettre à jour:
|
||
|
||
```bash
|
||
~/Documents/venv liveboxmonitor ❯ pip3 install -U pip setuptools wheel
|
||
Requirement already satisfied: pip in ./liveboxmonitor/lib/python3.10/site-packages (23.0)
|
||
Requirement already satisfied: setuptools in ./liveboxmonitor/lib/python3.10/site-packages (67.2.0)
|
||
Collecting setuptools
|
||
```
|
||
|
||
Copier LiveboxMonitor dans le venv:
|
||
|
||
```bash
|
||
~/Downloads/LiveboxMonitor $ cp -R * ~/Documents/venv/liveboxmonitor
|
||
```
|
||
|
||
Installer les dépendances:
|
||
|
||
```bash
|
||
~/Documents/venv/liveboxmonitor
|
||
liveboxmonitor ❯ pip3 install -r requirements.txt
|
||
Collecting PyQt6
|
||
```
|
||
|
||
Créer un exécutable avec [PyInstaller](https://pyinstaller.org/en/stable/index.html):
|
||
|
||
```bash
|
||
pyinstaller --paths liveboxmonitor/lib/python3.10/site-packages LiveboxMonitor.py
|
||
|
||
# Ne marche pas: PyQt6 est installé dans /opt/homebrew/lib
|
||
```
|
||
|
||
https://stackoverflow.com/questions/55312146/how-to-include-only-needed-modules-in-pyinstaller#55312170
|