Mastering Pipenv: Simplify Python Dependency and Virtual Environment Management
This guide explains what Pipenv is, how to install it, its advantages and drawbacks, and provides step‑by‑step instructions for creating virtual environments, managing packages, sharing projects with a team, and integrating Pipenv into PyCharm.
Preface
In traditional mature solutions we usually create a virtual environment based on the current Python version and manage packages with pip. Pipenv is a convenient tool that helps manage packages more easily.
What is Pipenv and what does it do?
Pipenv is a Python package management tool created by Kenneth Reitz, the author of
requests. It provides management across Python versions and packages, similar to npm or yarn in the frontend world.
It automatically creates and manages a virtual environment for a project. When you use Pipenv, it creates a
Pipfilein the project root to record package versions, and a
Pipfile.lockto lock versions and dependencies, preventing build errors.
It mainly solves two problems:
No need to manually create a virtual environment with virtualenv and pip for the current Python interpreter.
Eliminates unordered maintenance of
requirements.txtby using
Pipfileand
Pipfile.lockinstead.
Basic concepts:
Running
pipenv installin a new project root automatically creates a virtual environment and generates a
Pipfile.
If the
installcommand is run without specifying packages and a
Pipfileexists, Pipenv installs all packages listed under
[packages]in the
Pipfile.
Installing Pipenv
<code># MacOS
pip install pipenv
# Enable shell completion
echo 'eval "$(pipenv --completion)"' >> ~/.zshrc
# For CentOS 7, use /etc/profile or another env file instead of ~/.zshrc
</code>Pros and Cons of Pipenv
Advantages:
Automatically links the project to its virtualenv, allowing quick activation.
Provides a
Pipfileand
Pipfile.lockas a replacement for pip and a dependency list.
Pipfilesupports fixed PyPI source URLs and a specific Python version.
Supports a development‑dependency list;
pipenv installforces use of the source defined in the
Pipfile.
The
pipenv graphcommand displays the dependency tree.
Easy switching between Python 2 and Python 3.
Disadvantages:
On Windows the terminal prompt does not show the virtual‑env name, making it easy to lose track of the active environment.
Once a source is permanently set in the
Pipfile, the file still shows the official source even though the configured source is used.
Using Pipenv
Creating a Pipenv virtual environment
<code># Create project directory
mkdir project1
cd project1
# Optionally specify Python version
pipenv --python 3.10.4
# Initialize Pipfile and Pipfile.lock
pipenv install
# List packages (without entering the env)
pipenv run pip list
# Activate the virtual environment
pipenv shell
# Install a package
pipenv install requests
# Show dependency graph
pipenv graph
# Update a package
pipenv update requests
# Exit the environment
exit
# Remove the virtual environment (project files remain)
pipenv --rm
</code>Team Sharing
When sharing a project with a teammate, copy the
Pipfileand
Pipfile.lockinto the new directory and run:
<code>mkdir project2
cd project2
pipenv install
</code>This installs all required dependencies.
Using Pipenv in PyCharm
First add Pipenv to your PATH:
<code>vi ~/.zshrc
# python pipenv
export PATH="$PATH:/Users/allenjol/.local/" >> ~/.zshrc
source ~/.zshrc
</code>In PyCharm, create a new project and select “New environment using Pipenv”. The IDE will set up the Pipenv environment automatically.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.