Pipenv
Table of Contents
Python dependency management tool combining pip and virtualenv.
Creates and manages a virtual environment per project, tracks
dependencies in Pipfile and Pipfile.lock. Alternative to
manually managing venv + requirements.txt.
Note: largely superseded in new projects by uv (faster) or
poetry (richer feature set) — but still widely encountered
in existing projects.
Related: Python
Installation
pip install pipenv --break-system-packages # or brew install pipenv
Core Workflow
# Start a new project mkdir myproject && cd myproject pipenv install # creates Pipfile and venv # Install packages pipenv install requests # production dependency pipenv install pytest --dev # development-only dependency pipenv install "requests>=2.28" # with version constraint # Remove a package pipenv uninstall requests # Activate the virtual environment pipenv shell # Exit: type 'exit' or Ctrl+D # Run a command without activating pipenv run python script.py pipenv run pytest # Show installed packages and dependencies pipenv graph # Check for known security vulnerabilities pipenv check
Pipfile
[[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [packages] requests = ">=2.28" flask = "*" [dev-packages] pytest = "*" black = "*" [requires] python_version = "3.11"
Lockfile and Reproducibility
# Generate Pipfile.lock from current Pipfile pipenv lock # Install exact versions from Pipfile.lock (CI/production) pipenv install --ignore-pipfile # Install including dev packages pipenv install --dev # Sync environment to match Pipfile.lock exactly pipenv sync
Environment Variables
Pipenv loads a .env file in the project root automatically
when pipenv shell or pipenv run is used:
# .env DATABASE_URL=postgres://localhost/mydb SECRET_KEY=dev-secret-key DEBUG=true
Locating the Virtual Environment
# Show path to the virtual environment pipenv --venv # Show path to Python interpreter pipenv --py # Remove the virtual environment pipenv --rm
Tips
- Commit
PipfileandPipfile.lock—Pipfiledefines intent,Pipfile.lockensures reproducibility - Use
pipenv runin scripts and CI rather than activating the shell — more explicit and avoids environment state issues pipenv checkwrapspip audit— worth running before deploying to catch known-vulnerable dependency versions- If you prefer
venv+requirements.txt, generate one with:pipenv requirements > requirements.txt