-
Python > mise à jour d’un module
via apt
Example, you can install
requests
like this:sudo apt install python3-requests
Attention, la dernière version ne sera pet-être pas celle de PyPi…
via pip et venv
Exemple avec venv :
sudo apt install python3-venv
Pour créer un environement virtuel dans le dossier du projet:
python3 -m venv .venv source .venv/bin/activate
This modifies your PATH environment variable to include .venv/bin/.
Now you can install PyPI packages using pip into your virtual envirnoment (in this case .venv/), like this:
pip install requests
Pour ne pas activer / désactiver venv, you can run
pip
andpython
directly from the virtual environment, like this:.venv/bin/pip install requests .venv/bin/python >>> import requests
via pipx
pipx is a great tool for install command-line applications straight from PyPI :
sudo apt install pipx
Make sure ~/.local/bin/ is in your PATH environment variable :
pipx ensurepath
Close your terminal and open it again for the changes to take effect.
Now you can install a command-line application from PyPI. Behind the scenes, pipx will set up a virtual environment for each application, with its dependencies, completely isolated from the rest of the system to prevent problems. It’s brilliant. Here’s an example:
$ pipx install ruff $ ruff --help
via --break-system-packages
pip install --break-system-packages requests
Never remove or rename /usr/lib/python3.12/EXTERNALLY-MANAGED. It is there to stop you from breaking your system. You may not notice that your system is broken until weeks or months later, and you won’t understand at that point why it broke. If you must ignore these protections, you can do it on a one-off basis using --break-system-packages.
The only time I used
--break-system-packages
option was to install pipenv, which is what I use to create a virtual environment. pipenv from official distribution can be quite old. Once you have a virtual environment, you’re good to install anything else you need without worrying about "externally managed environment".