Skip to content
Shamik edited this page Jul 14, 2024 · 2 revisions

Python Package Dependency Management with Pipenv

Installation

Official Installation guide

Installing pipenv in the home directory to use it as global utility. Not many programs should be installed in the following manner and only programs such as poetry, virtualenv, pipenv, tox, can be installed in this manner. If you install too many programs with pip user install then it will lead to dependency conflicts.

pip install --user pipenv

If pipenv isn't available in your shell post installing it, then add the user's base binary directory to PATH. To find out the user base binary in Ubuntu python -m site --user-base. This will generally return ~/.local(~ will be replaced by your home path). Adding bin to the end of this(~/.local/bin) is your user base binary directory, which needs to be added to PATH. To know how to add the user base binary to your PATH, refer here.

Default pipenv venv path

By default pipenv creates all venv and stores it under ~/.local/share/virtualenvswith the name of the project’s root directory plus the hash of the full path to the project’s root (e.g., my_project-a3de50). If you change your project’s path, you break such a default mapping and pipenv will no longer be able to find and to use the project’s virtualenv. To make sure that you can change your project's path and use the project's venv, do the below.

Adding the venv default path to the project directory

This entire documentation is based on creating the venv inside the project directory.

Pipenv doesn't automatically save the venv in the project directory. However, it can be set to do so temporarily or permanently by using or adding the same to ~/.bashrc. If you don't know how, refer here.

export PIPENV_VENV_IN_PROJECT=1

Upgarding pipenv

pip install --user -U pipenv

Using pipenv in a new project directory

If you are starting afresh...

Creating a venv with pipenv

cd into a project directory and create a venv with a specific version of python with pipenv --python 3.7. NOTE: This requires that you have pyenv installed, the specific python installed in your system, or you have it installed as part of a venv. If you would want to install multiple versions of python without installing a package manager then install pyenv. This will automatically create a venv for the project and house the venv under .venv. This will also automatically activate the newly created venv. To activate any venv manually source .venv/bin/activate. To deactivate deactivate. However, one can also create automatically a venv with the default system python version, e.g in my case Python 3.10.6, by using pipenv shell.

Checking the venv

pipenv --venv

Removing a venv

pipenv --rm

Installing packages for the project

pipenv install requests flask==0.12.1

pipenv automatically creates a Pipfile and adds the project packages under [packages]. It will also generate a Pipfile.lock if it's not generated already and will keep all the detailed packages dependency information.

Pipfile Project Packages

Installing packages for the development of the project

Sometimes, while developing we might like to have additional packages installed which aren't useful for running the project, for instance a code formatter like autopep8, a documentation generator like Sphinx, and a static analysis tool like pycodestyle, Pylint, Flake8, mypy, or coverage.py.

pipenv install pytest autopep8 pycodestyle -d

pipenv adds the devlopment packages under [dev-packages] and creates a Pipfile if necessary. It will also generate a Pipfile.lock if it's not generated already and will keep all the detailed packages dependency information.

Pipfile Development Packages

Uninstalling a package

pipenv uninstall requests

Removing all packages

pipenv uninstall --all

Contents of Pipfile

A Pipfile contains minimal information about packages and the environment details. For this trial project it looks like below.

Sample Pipfile

Contents of Pipfile.lock

The Pipfile.lock file contains detailed installed package information(package versions) along with their dependencies. If this file is present, pipenv will always rely on this file to install the dependencies.

The meta tag contains the environment information.

Sample Pipfile.lock meta

The default tag contains the detailed project packages information.

Sample Pipfile.lock project

The develop tag contains the detailed development packages information.

Sample Pipfile.lock development

Creating a production environment with Pipfile.lock

Instead of creating a production environment with Pipfile, we create a deterministic environment with Pipfile.lock.

pipenv install --ignore-pipfile

Removing the option --ignore-pipfile from the above command will install all the packages from the Pipfile, however, the exact environment might not be created as some packages in the Pipfile don't have a specific version to create a deterministic build. While creating the Pipfile, the project might have used a specific version of requests package, but there might be an updated version of the package now, which will be installed by default as the Pipfile doesn't restrict requests package to be installed with a specific version.

A developer setting up the same project

A developer might want to set up the same environment but would also like to install the development packages as well as he/she would like to develop the project.

pipenv install --ignore-pipfile -d

Checking package dependencies

pipenv graph

Running a command with pip

pipenv run pip list

There are many commands one can run with pipenv run.

Checking for package vulnerability

pipenv check

Setting up a venv with project requirements.txt

If a project requirements.txt file already exists.

pipenv install -r requirements.txt

Setting up a venv with dev requirements.txt

If a development requirements.txt file already exists.

pipenv install -r requirements_dev.txt -d

Generating a project requirements.txt file

pipenv requirements > requirements.txt

Generating a project and dev requirements.txt file

pipenv requirements --dev > requirements_w_dev.txt

Generating a development only requirements.txt file

pipenv requirements --dev-only > requirements_only_dev.txt

Commands Usage

https://pipenv.pypa.io/en/latest/commands.html

Syncing

pipenv sync #installs dependencies from the Pipfile.lock without altering it

Locking

pipenv lock #updates all dependencies in Pipfile.lock

Updating a package along with the environment

pipenv update <package> #updates the lock of a specified dependency and sub-dependencies

Updating a package along without the environment

pipenv upgrade <package> #updates the lock of a specified dependency without modifying the environment.

Activating an environment

pipenv shell #spawns a shell with the virtual environment activated

Showing Package Dependency graph

pipenv graph #shows a dependency graph.
Clone this wiki locally