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 Pipfile in the project root to record package versions, and a Pipfile.lock to 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.txt by using Pipfile and Pipfile.lock instead.
Basic concepts:
Running pipenv install in a new project root automatically creates a virtual environment and generates a Pipfile.
If the install command is run without specifying packages and a Pipfile exists, Pipenv installs all packages listed under [packages] in the Pipfile.
Installing Pipenv
# MacOS
pip install pipenv
# Enable shell completion
echo 'eval "$(pipenv --completion)"' >> ~/.zshrc
# For CentOS 7, use /etc/profile or another env file instead of ~/.zshrcPros and Cons of Pipenv
Advantages:
Automatically links the project to its virtualenv, allowing quick activation.
Provides a Pipfile and Pipfile.lock as a replacement for pip and a dependency list. Pipfile supports fixed PyPI source URLs and a specific Python version.
Supports a development‑dependency list; pipenv install forces use of the source defined in the Pipfile.
The pipenv graph command 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
# 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 --rmTeam Sharing
When sharing a project with a teammate, copy the Pipfile and Pipfile.lock into the new directory and run:
mkdir project2
cd project2
pipenv installThis installs all required dependencies.
Using Pipenv in PyCharm
First add Pipenv to your PATH:
vi ~/.zshrc
# python pipenv
export PATH="$PATH:/Users/allenjol/.local/" >> ~/.zshrc
source ~/.zshrcIn PyCharm, create a new project and select “New environment using Pipenv”. The IDE will set up the Pipenv environment automatically.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
