Ce sujet est ma tentative de faire connaître pipenv dont on parle peu, j’ai l’impression, alors même qu’on continue de parler de venv et de comment lui tordre le bras — parce que, oui, venv a des défauts notoires. Il serait grand temps que venv prenne la retraite pour laisser place à ce qui se fait de mieux, actuellement.
pipenv : petite intro
Extrait de la note d’intention (non traduite en français) publiée sur le site officiel du projet pipenv :
Pipenv: Python Dev Workflow for Humans
Pipenv is a Python virtualenv management tool that supports a multitude of systems and nicely bridges the gaps between pip, python (using system python, pyenv or asdf) and virtualenv. Linux, macOS, and Windows are all first-class citizens in pipenv.
Pipenv automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your
Pipfileas you install/uninstall packages. It also generates a projectPipfile.lock, which is used to produce deterministic builds.Pipenv is primarily meant to provide users and developers of applications with an easy method to arrive at a consistent working project environment.
The problems that Pipenv seeks to solve are multi-faceted:
You no longer need to use
pipandvirtualenvseparately: they work together.Managing a
requirements.txtfile with package hashes can be problematic. Pipenv usesPipfileandPipfile.lockto separate abstract dependency declarations from the last tested combination.Hashes are documented in the lock file which are verified during install. Security considerations are put first.
Strongly encourage the use of the latest versions of dependencies to minimize security risks arising from outdated components.
Gives you insight into your dependency graph (e.g.
$ pipenv graph).Streamline development workflow by supporting local customizations with
.envfiles.
Pour aller plus loin :
Un peu de pratique : installer pipenv dans le cadre d’un projet
Petites précisions pour la clarté :
- Le texte ci-dessous est un extrait des notes perso, prises en suivant un tuto de Django.
- Les notes sont en anglais (pas taper ! c’est pas de l’anglais littéraire non plus…)
1. Why pipenv? pipenv vs venv
pipenv has some advantages over venv, like:
| Feature | pipenv | venv |
|---|---|---|
| Dependency management | Uses a Pipfile to track and manage package dependencies | Does not have built-in dependency management |
| Reproducibility | Can create a Pipfile.lock file to recreate your virtual environment on another machine or at a later time | Does not have a built-in mechanism for reproducibility |
| Simplicity | Combines the functionality of venv and pip into a single tool | Is a lower-level tool that only creates virtual environments |
2. Set up pipenv virtual environment
2.1. Make sure pipenv is installed, otherwise install it
sudo apt update
sudo apt install pipenv
2.2. Open terminal in a so-called my-django-workspace directory and run pipenv install django
cd my-django-workspace
pipenv install django
This will create
-
./Pipfile(a TOML config file) -
./Pipfile.lock(a JSON file)
and a few new directories in your user’s home directory:
~/.local/share/virtualenv/
├── py_info/
└── wheel/
~/.local/share/virtualenvs/
└── my-django-workspace-A3MO-irQ/
2.3. Run pipenv shell to launch the virtual environment
-
Make sure you are in a directory with the
PipfileandPipfile.lockfiles. -
Run
pipenv shell -
To quit the virtual environment, press ctrl d.
2.4. Other useful pipenv commands
pipenv run pip freeze