コンテンツにスキップ

Pipenv Cheatsheet

Pipenv Cheatsheet

Installation

PlatformCommand
Ubuntu/Debiansudo apt install pipenv or pip install --user pipenv
Fedora/RHELsudo dnf install pipenv or pip install --user pipenv
Arch Linuxsudo pacman -S python-pipenv
macOSbrew install pipenv or pip install --user pipenv
Windowspip install --user pipenv or choco install pipenv
Verify Installationpipenv --version
Post-Installation (if using —user):
# Linux/macOS - Add to PATH
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Basic Commands

CommandDescription
pipenv --threeCreate new project with Python 3
pipenv --python 3.11Create project with specific Python version
pipenv installInstall all dependencies from Pipfile
pipenv install requestsInstall a package and add to Pipfile
pipenv install pytest --devInstall package as dev dependency
pipenv install -r requirements.txtInstall from requirements.txt file
pipenv uninstall requestsRemove a package
pipenv uninstall --allRemove all packages
pipenv shellActivate virtual environment
exitDeactivate virtual environment (inside shell)
pipenv run python script.pyRun command without activating shell
pipenv --venvShow virtualenv path
pipenv --whereShow project directory path
pipenv --pyShow Python interpreter path
pipenv --rmRemove virtual environment
pipenv lockGenerate/update Pipfile.lock
pipenv syncInstall from Pipfile.lock (production)
pipenv updateUpdate all packages to latest versions
pipenv listList installed packages
pipenv graphShow dependency tree
pipenv checkCheck for security vulnerabilities

Advanced Usage

CommandDescription
pipenv install "django>=4.0,<5.0"Install with version constraints
pipenv install -e .Install local package in editable mode
pipenv install -e git+https://github.com/user/repo.git#egg=pkgInstall from Git repository
pipenv install "requests[security]"Install package with extras
pipenv install --skip-lockInstall without updating lock file (faster)
pipenv sync --devSync including dev dependencies
pipenv cleanRemove packages not in Pipfile.lock
pipenv lock --clearClear and regenerate lock file
pipenv lock --preLock including pre-release versions
pipenv update requestsUpdate specific package only
pipenv update --outdatedShow outdated packages
pipenv graph --reverseShow reverse dependency tree
pipenv graph --jsonOutput dependency graph as JSON
pipenv check --verboseDetailed security vulnerability check
pipenv requirements > requirements.txtExport to requirements.txt format
pipenv requirements --devExport dev requirements
pipenv run startRun script defined in Pipfile
PIPENV_VENV_IN_PROJECT=1 pipenv installCreate .venv in project directory
pipenv --python /usr/bin/python3.9Use specific Python executable
pipenv lock --keep-outdatedLock without updating existing packages

Configuration

Pipfile Structure

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
django = ">=4.0,<5.0"
requests = "~=2.28.0"
psycopg2-binary = "*"
celery = {extras = ["redis"], version = ">=5.0"}
mypackage = {editable = true, path = "."}
private-pkg = {git = "ssh://git@github.com/user/repo.git", ref = "main"}

[dev-packages]
pytest = "*"
black = "==23.1.0"
flake8 = "*"
mypy = "*"

[requires]
python_version = "3.11"

[scripts]
start = "python manage.py runserver"
test = "pytest tests/ -v"
lint = "flake8 src/"
format = "black src/"

Environment Variables

# Virtualenv location
export PIPENV_VENV_IN_PROJECT=1           # Create .venv in project
export WORKON_HOME=~/.virtualenvs         # Custom virtualenv location

# Timeout settings
export PIPENV_TIMEOUT=300                 # Lock timeout (seconds)
export PIPENV_INSTALL_TIMEOUT=900         # Install timeout

# Behavior settings
export PIPENV_SKIP_LOCK=1                 # Skip lock generation
export PIPENV_NOSPIN=1                    # Disable spinner (for CI)
export PIPENV_HIDE_EMOJIS=1              # Disable emojis (for CI)
export PIPENV_YES=1                       # Auto-yes to prompts

# Python version
export PIPENV_PYTHON=3.11                 # Default Python version

# Custom PyPI mirror
export PIPENV_PYPI_MIRROR=https://pypi.org/simple

.env File Support

Pipenv automatically loads .env file in project root:

# .env file example
DATABASE_URL=postgresql://localhost/mydb
SECRET_KEY=your-secret-key
DEBUG=True

Common Use Cases

Use Case: Starting a New Django Project

# Initialize project with Python 3.11
pipenv --python 3.11

# Install Django and production dependencies
pipenv install django psycopg2-binary gunicorn

# Install development tools
pipenv install pytest pytest-django black --dev

# Activate environment and start project
pipenv shell
django-admin startproject myproject .

Use Case: Deploying to Production

# On development machine - lock dependencies
pipenv lock

# Commit Pipfile and Pipfile.lock to version control
git add Pipfile Pipfile.lock
git commit -m "Lock dependencies"

# On production server - install exact versions
pipenv sync

# Run application
pipenv run gunicorn myapp.wsgi:application

Use Case: Working with Team Members

# Clone repository
git clone https://github.com/team/project.git
cd project

# Install all dependencies (including dev)
pipenv install --dev

# Activate environment
pipenv shell

# Run tests
pytest

# When adding new package, lock and commit
pipenv install new-package
git add Pipfile Pipfile.lock
git commit -m "Add new-package dependency"

Use Case: CI/CD Pipeline

# In CI configuration (e.g., .gitlab-ci.yml, GitHub Actions)
# Install pipenv
pip install pipenv

# Install dependencies without dev packages
pipenv sync

# Run tests
pipenv run pytest

# Check for security vulnerabilities
pipenv check

# Set CI-friendly environment variables
export PIPENV_NOSPIN=1
export PIPENV_HIDE_EMOJIS=1

Use Case: Managing Multiple Environments

# Development environment
pipenv install --dev
pipenv run python manage.py runserver

# Testing environment
pipenv sync --dev
pipenv run pytest

# Production environment
pipenv sync  # Only production dependencies
pipenv run gunicorn app:app

# Staging with specific configuration
pipenv run --env-file=.env.staging python app.py

Best Practices

  • Always commit both Pipfile and Pipfile.lock to version control for reproducible builds across environments
  • Use pipenv sync in production instead of pipenv install to ensure exact versions from Pipfile.lock are installed
  • Separate dev and production dependencies using --dev flag to keep production environments lean
  • Run pipenv check regularly to scan for security vulnerabilities in your dependencies
  • Use version constraints wisely - ~= for compatible releases, >=,< for ranges, == only when necessary
  • Create virtualenv in project directory by setting PIPENV_VENV_IN_PROJECT=1 for easier IDE integration
  • Use pipenv run for one-off commands instead of activating shell when running scripts in automation
  • Define common scripts in Pipfile under [scripts] section for team consistency
  • Keep Pipfile.lock updated by running pipenv lock after modifying Pipfile manually
  • Use .env files for environment-specific configuration - Pipenv loads them automatically

Troubleshooting

IssueSolution
pipenv: command not foundAdd ~/.local/bin to PATH or reinstall with pip install --user pipenv
Lock operation takes too longIncrease timeout: export PIPENV_TIMEOUT=600 or use --skip-lock for faster installs
Dependency resolution conflictsTry pipenv lock --clear to regenerate lock file from scratch
Virtual environment not activatingCheck pipenv --venv shows path, try pipenv --rm and recreate with pipenv install
Package not found in Pipfile.lockRun pipenv lock to regenerate lock file with new dependencies
Wrong Python version usedSpecify version explicitly: pipenv --python 3.11 or check [requires] in Pipfile
SSL certificate verification errorsSet verify_ssl = false in Pipfile [[source]] section (not recommended for production)
Pipenv installs system-wide packagesEnsure PIPENV_IGNORE_VIRTUALENVS is not set, or unset it
Can’t find locally installed packageInstall in editable mode: pipenv install -e .
pipenv check fails in CIUse pipenv check --ignore VULNERABILITY_ID to ignore specific known issues
Slow package installationUse a PyPI mirror: export PIPENV_PYPI_MIRROR=https://mirror.url/simple
Conflicting lock file on teamEnsure all team members use same Python version specified in [requires]

Version Specifiers Quick Reference

SpecifierMeaningExample
*Any versionrequests = "*"
==Exact versiondjango = "==4.2.0"
>=Greater than or equalflask = ">=2.0"
<=Less than or equalnumpy = "<=1.24"
~=Compatible releaserequests = "~=2.28.0" (>=2.28.0, <2.29.0)
>=,<Version rangedjango = ">=4.0,<5.0"