Pipenv Cheatsheet
Installation
| Platform | Command |
|---|
| Ubuntu/Debian | sudo apt install pipenv or pip install --user pipenv |
| Fedora/RHEL | sudo dnf install pipenv or pip install --user pipenv |
| Arch Linux | sudo pacman -S python-pipenv |
| macOS | brew install pipenv or pip install --user pipenv |
| Windows | pip install --user pipenv or choco install pipenv |
| Verify Installation | pipenv --version |
| Post-Installation (if using —user): | |
# Linux/macOS - Add to PATH
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Basic Commands
| Command | Description |
|---|
pipenv --three | Create new project with Python 3 |
pipenv --python 3.11 | Create project with specific Python version |
pipenv install | Install all dependencies from Pipfile |
pipenv install requests | Install a package and add to Pipfile |
pipenv install pytest --dev | Install package as dev dependency |
pipenv install -r requirements.txt | Install from requirements.txt file |
pipenv uninstall requests | Remove a package |
pipenv uninstall --all | Remove all packages |
pipenv shell | Activate virtual environment |
exit | Deactivate virtual environment (inside shell) |
pipenv run python script.py | Run command without activating shell |
pipenv --venv | Show virtualenv path |
pipenv --where | Show project directory path |
pipenv --py | Show Python interpreter path |
pipenv --rm | Remove virtual environment |
pipenv lock | Generate/update Pipfile.lock |
pipenv sync | Install from Pipfile.lock (production) |
pipenv update | Update all packages to latest versions |
pipenv list | List installed packages |
pipenv graph | Show dependency tree |
pipenv check | Check for security vulnerabilities |
Advanced Usage
| Command | Description |
|---|
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=pkg | Install from Git repository |
pipenv install "requests[security]" | Install package with extras |
pipenv install --skip-lock | Install without updating lock file (faster) |
pipenv sync --dev | Sync including dev dependencies |
pipenv clean | Remove packages not in Pipfile.lock |
pipenv lock --clear | Clear and regenerate lock file |
pipenv lock --pre | Lock including pre-release versions |
pipenv update requests | Update specific package only |
pipenv update --outdated | Show outdated packages |
pipenv graph --reverse | Show reverse dependency tree |
pipenv graph --json | Output dependency graph as JSON |
pipenv check --verbose | Detailed security vulnerability check |
pipenv requirements > requirements.txt | Export to requirements.txt format |
pipenv requirements --dev | Export dev requirements |
pipenv run start | Run script defined in Pipfile |
PIPENV_VENV_IN_PROJECT=1 pipenv install | Create .venv in project directory |
pipenv --python /usr/bin/python3.9 | Use specific Python executable |
pipenv lock --keep-outdated | Lock 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
| Issue | Solution |
|---|
pipenv: command not found | Add ~/.local/bin to PATH or reinstall with pip install --user pipenv |
| Lock operation takes too long | Increase timeout: export PIPENV_TIMEOUT=600 or use --skip-lock for faster installs |
| Dependency resolution conflicts | Try pipenv lock --clear to regenerate lock file from scratch |
| Virtual environment not activating | Check pipenv --venv shows path, try pipenv --rm and recreate with pipenv install |
| Package not found in Pipfile.lock | Run pipenv lock to regenerate lock file with new dependencies |
| Wrong Python version used | Specify version explicitly: pipenv --python 3.11 or check [requires] in Pipfile |
| SSL certificate verification errors | Set verify_ssl = false in Pipfile [[source]] section (not recommended for production) |
| Pipenv installs system-wide packages | Ensure PIPENV_IGNORE_VIRTUALENVS is not set, or unset it |
| Can’t find locally installed package | Install in editable mode: pipenv install -e . |
pipenv check fails in CI | Use pipenv check --ignore VULNERABILITY_ID to ignore specific known issues |
| Slow package installation | Use a PyPI mirror: export PIPENV_PYPI_MIRROR=https://mirror.url/simple |
| Conflicting lock file on team | Ensure all team members use same Python version specified in [requires] |
Version Specifiers Quick Reference
| Specifier | Meaning | Example |
|---|
* | Any version | requests = "*" |
== | Exact version | django = "==4.2.0" |
>= | Greater than or equal | flask = ">=2.0" |
<= | Less than or equal | numpy = "<=1.24" |
~= | Compatible release | requests = "~=2.28.0" (>=2.28.0, <2.29.0) |
>=,< | Version range | django = ">=4.0,<5.0" |