Saltar a contenido

Pipenv

Pipenv Cheatsheet

Instalación

__TABLE_114_ **Pos-Instalación (si usa --usuario): #

# Linux/macOS - Add to PATH
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Comandos básicos

Command Description
INLINE_CODE_19 Create new project with Python 3
INLINE_CODE_20 Create project with specific Python version
INLINE_CODE_21 Install all dependencies from Pipfile
INLINE_CODE_22 Install a package and add to Pipfile
INLINE_CODE_23 Install package as dev dependency
INLINE_CODE_24 Install from requirements.txt file
INLINE_CODE_25 Remove a package
INLINE_CODE_26 Remove all packages
INLINE_CODE_27 Activate virtual environment
INLINE_CODE_28 Deactivate virtual environment (inside shell)
INLINE_CODE_29 Run command without activating shell
INLINE_CODE_30 Show virtualenv path
INLINE_CODE_31 Show project directory path
INLINE_CODE_32 Show Python interpreter path
INLINE_CODE_33 Remove virtual environment
INLINE_CODE_34 Generate/update Pipfile.lock
INLINE_CODE_35 Install from Pipfile.lock (production)
INLINE_CODE_36 Update all packages to latest versions
INLINE_CODE_37 List installed packages
INLINE_CODE_38 Show dependency tree
INLINE_CODE_39 Check for security vulnerabilities

Advanced Usage

Command Description
INLINE_CODE_40 Install with version constraints
INLINE_CODE_41 Install local package in editable mode
INLINE_CODE_42 Install from Git repository
INLINE_CODE_43 Install package with extras
INLINE_CODE_44 Install without updating lock file (faster)
INLINE_CODE_45 Sync including dev dependencies
INLINE_CODE_46 Remove packages not in Pipfile.lock
INLINE_CODE_47 Clear and regenerate lock file
INLINE_CODE_48 Lock including pre-release versions
INLINE_CODE_49 Update specific package only
INLINE_CODE_50 Show outdated packages
INLINE_CODE_51 Show reverse dependency tree
INLINE_CODE_52 Output dependency graph as JSON
INLINE_CODE_53 Detailed security vulnerability check
INLINE_CODE_54 Export to requirements.txt format
INLINE_CODE_55 Export dev requirements
INLINE_CODE_56 Run script defined in Pipfile
INLINE_CODE_57 Create .venv in project directory
INLINE_CODE_58 Use specific Python executable
INLINE_CODE_59 Lock without updating existing packages

Configuración

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 carga automáticamente .env_ archivo en raíz del proyecto:

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

Common Use Cases

Use Case: Begin 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

Buenas prácticas

  • Siempre comprometer Pipfile y Pipfile.lock a control de versiones para las construcciones reproducibles a través de entornos
  • Use pipenv sync en producción en lugar de pipenv install para asegurar versiones exactas de Pipfile. bloqueo instalado
  • Dev y dependencias de producción separadas utilizando la bandera __INLINE_CODE_63_ para mantener los entornos de producción inclinados Run pipenv check_ regularmente para escanear vulnerabilidades de seguridad en sus dependencias
  • Utilice las restricciones de la versión con prudencia - ~= para versiones compatibles, >=,< para rangos, == sólo cuando sea necesario Crear virtualenv en el directorio del proyecto mediante el ajuste PIPENV_VENV_IN_PROJECT=1_ para facilitar la integración de IDE
  • Use pipenv run para comandos one-off en lugar de activar shell al ejecutar scripts en automatización
  • Definir scripts comunes en Pipfile bajo [scripts] sección para la consistencia del equipo
  • Keep Pipfile.lock actualizado ejecutando __INLINE_CODE_71_ después de modificar Pipfile manualmente
  • Uso .env_ para la configuración específica del entorno - Pipenv los carga automáticamente

Troubleshooting

Issue Solution
INLINE_CODE_73 Add INLINE_CODE_74 to PATH or reinstall with INLINE_CODE_75
Lock operation takes too long Increase timeout: INLINE_CODE_76 or use INLINE_CODE_77 for faster installs
Dependency resolution conflicts Try INLINE_CODE_78 to regenerate lock file from scratch
Virtual environment not activating Check INLINE_CODE_79 shows path, try INLINE_CODE_80 and recreate with INLINE_CODE_81
Package not found in Pipfile.lock Run INLINE_CODE_82 to regenerate lock file with new dependencies
Wrong Python version used Specify version explicitly: INLINE_CODE_83 or check INLINE_CODE_84 in Pipfile
SSL certificate verification errors Set INLINE_CODE_85 in Pipfile INLINE_CODE_86 section (not recommended for production)
Pipenv installs system-wide packages Ensure INLINE_CODE_87 is not set, or unset it
Can't find locally installed package Install in editable mode: INLINE_CODE_88
INLINE_CODE_89 fails in CI Use INLINE_CODE_90 to ignore specific known issues
Slow package installation Use a PyPI mirror: INLINE_CODE_91
Conflicting lock file on team Ensure all team members use same Python version specified in INLINE_CODE_92

Version Specifiers Quick Reference

TABLE_118_ _!= Silencio Excluir la versión package = "!=1.0.0"