Fundamentals 11 min read

A Comprehensive Guide to Using Pipenv for Python Dependency and Virtual Environment Management

This article introduces pipenv, explains its concepts, installation methods, virtual environment creation, package management, and commands for handling dependencies, generating Pipfiles, and removing environments, providing a comprehensive tutorial for Python developers in practice.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
A Comprehensive Guide to Using Pipenv for Python Dependency and Virtual Environment Management

Follow + Star, Learn New Python Skills Daily

Preparation

Set up the trial environment and related documentation.

Environment

pipenv 9.0.1

python3.6

python2.7

Documentation

GitHub repository: pipenv

Official pipenv documentation

Pipenv Basic Concepts

1. Previously we used pip + virtualenv to manage Python dependencies, distinguishing Python versions with the --python flag (instead of pyenv, reducing dependencies). Pipenv unifies these tools, allowing you to replace them with a single command. 2. Pipenv uses a Pipfile instead of requirements.txt to record packages. 3. It adds a Pipfile.lock to lock package names, versions, and dependency graphs. 4. Inspired by package managers such as bundler, composer, npm, cargo, and yarn, Pipenv brings the best practices to Python.

Pipenv Feature Trial

Installation

Standard Installation

Pipenv can be installed directly with pip.

<code>pip install pipenv</code>

It is recommended to install under Python 3 for better compatibility with virtualenv.

<code>The use of Python <span style="color: rgb(223, 83, 32)">3</span> is highly preferred over Python <span style="color: rgb(223, 83, 32)">2</span>, when installing Pipenv. Compatibility with three virtualenvs is greatly improved when using Python <span style="color: rgb(223, 83, 32)">3</span> — Kenneth Reitz</code>

User‑Mode Installation

To avoid affecting the system Python libraries, install in user mode.

<code>pip install --user pipenv</code>

The default installation path is /usr/local/lib/python2.7/site-packages ; in user mode the packages are stored in the user library, e.g., /Users/pylixm/Library/Python/3.6/lib/python/site-packages . Add the binary directory to PATH if pipenv is not found.

Pipenv Usage

Initialize Virtual Environment

Run pipenv install to create a virtual environment.

<code>~/laboratory/pip_test_project $ pipenv install
Creating a virtualenv for this project…
New python executable in /Users/pylixm/.local/share/virtualenvs/pip_test_project-MXA0TC90/bin/python2.7
Also creating executable in /Users/pylixm/.local/share/virtualenvs/pip_test_project-MXA0TC90/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /Users/pylixm/.local/share/virtualenvs/pip_test_project-MXA0TC90
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (c23e27)!
Installing dependencies from Pipfile.lock (c23e27)…
…
To activate this project's virtualenv, run the following:
 $ pipenv shell</code>

The environment is created under ~/.local/share/virtualenvs using the default Python 2.7. You can specify a Python version with --python 3.5 (requires that version to be installed) or --two / --three .

After initialization, two files appear in the project directory: Pipfile and Pipfile.lock . The Pipfile replaces requirements.txt and should be committed to version control. Pipfile.lock stores a hash of package names, versions, and dependencies to ensure integrity.

Install Python Packages

Normal Installation

Install the requests package:

<code>$ pipenv install requests
Installing requests…
Collecting requests
Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached certifi-2017.11.5-py2.py3-none-any.whl
Collecting idna>=2.5,<2.7 (from requests)
Using cached idna-2.6-py2.py3-none-any.whl
Collecting urllib3>=1.21.1,<1.23 (from requests)
Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting chardet>=3.0.2,<3.1.0 (from requests)
Using cached chardet-3.0.4-py2.py3-none-any.whl
Successfully installed certifi-2017.11.5 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

Adding requests to Pipfile's [packages]…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (2f8679)!</code>

View installed packages and their direct dependencies with pipenv graph :

<code>$ pipenv graph
requests==2.18.4
  - certifi [required: >=2017.4.17, installed: 2017.11.5]
  - chardet [required: <3.1.0,>=3.0.2, installed: 3.0.4]
  - idna [required: >=2.5,<2.7, installed: 2.6]
  - urllib3 [required: >=1.21.1,<1.23, installed: 1.22]</code>

Install Development Dependencies Only

Use the --dev flag to install only packages listed under [dev-packages] :

<code>pipenv install --dev requests --three</code>

The resulting Pipfile shows the separation between [dev-packages] and [packages] , and the [requires] section records the Python version used to build the environment.

Install from requirements.txt

<code>pipenv install -r requirements.txt</code>

This allows migration from an existing requirements.txt to a Pipenv‑managed environment.

Generate a requirements.txt from the current Pipenv lock file with:

<code>pipenv lock -r [--dev] > requirements.txt</code>

Run Inside the Virtual Environment

Execute a script using the environment:

<code>pipenv run python xxx.py</code>

Or spawn a shell inside the virtual environment:

<code>~/laboratory/pip_test_project $ pipenv shell --anyway
Spawning environment shell (/bin/zsh). Use 'exit' to leave.
source /Users/pylixm/.local/share/virtualenvs/pip_test_project-MXA0TC90/bin/activate
…
$ exit</code>

If the shell does not start, ensure the environment variable paths are correctly configured. Pipenv also supports an .env file in the project root to automatically load required environment variables when the shell is launched.

Delete Virtual Environment and Packages

Remove a package:

<code>pipenv uninstall requests</code>

Remove the entire virtual environment:

<code>pipenv --rm</code>

Summary

Pipenv elegantly solves Python package and version management.

It also manages inter‑package dependencies, simplifying the creation of reproducible development environments.

dependency managementpipenvvirtualenvenvironmentpipfile
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.