Master Python Dependency Management with Pipenv: A Step‑by‑Step Guide

This article explains how to simplify Python project dependency handling using Pipenv, covering installation, Pipfile management, development versus production packages, virtual environment activation, and practical command‑line examples for seamless workflow integration.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Dependency Management with Pipenv: A Step‑by‑Step Guide
Original link: https://robots.thoughtbot.com/how-to-manage-your-python-projects-with-pipenv Translator: Jiong

At thoughtbot we work primarily with Ruby and Rails, but we choose the best language or framework for each problem, and I’ve been exploring machine‑learning with Python lately.

Unlike Ruby, Python lacks a built‑in dependency manager like Bundler; developers typically use virtualenv together with a requirements.txt file and install packages via pip. This approach works but can become cumbersome when handling multiple environments or updating packages.

Pipenv, created by Kenneth Reitz, combines the functionality of pip, Pipfile, and virtualenv into a powerful CLI tool that streamlines dependency management.

Getting Started

First, install Pipenv: pip install pipenv Then navigate to your project directory and initialize Pipenv:

cd my_project
pipenv install

This creates Pipfile and Pipfile.lock in the project folder and sets up a new virtual environment (defaulting to the system Python version unless you specify --two or --three).

Managing Python Dependencies

Pipfile

replaces requirements.txt by storing package information. If you start Pipenv in a project that already has a requirements.txt, Pipenv will install the listed packages before removing the file.

To add a package, use the install command, e.g.: pipenv install beautifulsoup4 To remove a package, use uninstall: pipenv uninstall beautifulsoup4 Lock the current package versions with: pipenv lock When another developer clones the repository, they only need to run: pipenv install Pipenv will locate the Pipfile, create a virtual environment, and install all required packages.

Managing Your Development Environment

Packages needed only for development (e.g., testing tools) can be installed with the --dev flag, keeping them separate from production dependencies: pipenv install --dev nose2 Running pipenv install without --dev will skip development‑only packages, ensuring production builds stay lean.

Running Your Code

Activate the virtual environment and run commands with: pipenv run which python To execute your script: pipenv run python my_project.py You can create a shell alias for convenience, for example:

alias prp="pipenv run python"

Keep It Simple

I hope this guide has shown you how to manage Python projects with Pipenv. Although it’s still young, it already simplifies dependency handling compared to the traditional Ruby Bundler workflow, and I’ll continue to support its evolution.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonBackend Developmentdependency managementpipenv
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.