Vai al contenuto
__HTML_TAG_110_📄 Generare Pipenv PDF Guide_HTML_TAG_111__

Pipenv Cheatsheet

Installazione

Tabella_114_ **Post-Installation (se si utilizza --utente): E' una cosa da fare. Traduzione:

Comandi di base

Tabella_115

Uso avanzato

Tabella_116_

Configurazione

Struttura del file

[[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/"

Variabili ambientali

# 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 Supporto file

Carica automaticamente Pipenv .env file nella radice del progetto:

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

Common Use Cases

Use Case: Avviare un nuovo progetto Django

# 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: Lavorare con i membri del team

# 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: Gestione di ambienti multipli

# 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

Migliori Pratiche

  • ** Compiti sempre sia Pipfile che Pipfile.lock** per il controllo della versione per le costruzioni riproducibili in ambienti
  • Utilizza pipenv sync_ nella produzione invece di pipenv install per garantire versioni esatte da Pipfile. blocco sono installati
  • Separate dev and production dependencies usando la bandiera --dev_ per mantenere gli ambienti produttivi
  • Run pipenv check_ regolarmente per la scansione delle vulnerabilità di sicurezza nelle dipendenze
  • Utilizzare saggiamente i vincoli di versione - ~= per le versioni compatibili, >=,<_ per range, == solo se necessario
  • Create virtuale nella directory del progetto impostando PIPENV_VENV_IN_PROJECT=1 per una più facile integrazione IDE
  • Utilizza pipenv run per comandi one-off invece di attivare la shell durante l'esecuzione di script nell'automazione
  • ** Definire gli script comuni in Pipfile** sotto la sezione [scripts] per la consistenza del team
  • **Keep Pipfile.lock aggiornato ** eseguendo pipenv lock dopo aver modificato Pipfile manualmente
  • Utilizzare i file .env_ per la configurazione specifica dell'ambiente - Pipenv li carica automaticamente

Risoluzione dei problemi

Tabella_117_

Version Specifiers Quick Reference

Specifier Meaning Example
INLINE_CODE_93 Any version INLINE_CODE_94
INLINE_CODE_95 Exact version INLINE_CODE_96
INLINE_CODE_97 Greater than or equal INLINE_CODE_98
INLINE_CODE_99 Less than or equal INLINE_CODE_100
INLINE_CODE_101 Compatible release INLINE_CODE_102 (>=2.28.0, <2.29.0)
INLINE_CODE_103 Version range INLINE_CODE_104
!= Esclusa la versione package = "!=1.0.0"_