Gli ambienti virtuali sono la pietra angolare della gestione delle dipendenze Python. Questa guida copre i quattro strumenti principali: venv, conda, pipenv e Poetry.
Perché gli ambienti virtuali sono importanti
Ogni progetto Python ha le proprie dipendenze con requisiti di versione specifici. Gli ambienti virtuali risolvono questo problema creando installazioni Python isolate per progetto.
venv: Libreria standard integrata
venv è incluso con Python 3.3+ ed è l'opzione più semplice. Non richiede installazione aggiuntiva.
# venv — Built-in (Python 3.3+)
# Create a virtual environment
python -m venv .venv
# Activate (macOS/Linux)
source .venv/bin/activate
# Activate (Windows)
.venv\Scripts\activate
# Install packages
pip install django requests
# Save dependencies
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt
# Deactivate
deactivate
# Delete the environment
rm -rf .venv
# Recommended: add .venv to .gitignore
echo ".venv" >> .gitignorePoetry: Gestione moderna delle dipendenze
Poetry è il gestore di dipendenze Python più moderno e completo.
# Poetry — Modern Dependency Management (Recommended 2026)
# Install Poetry
curl -sSL https://install.python-poetry.org | python3 -
# Create a new project
poetry new my-project
cd my-project
# Add dependencies
poetry add django
poetry add --group dev pytest black ruff
# Install all dependencies (from poetry.lock)
poetry install
# Run commands in the virtual environment
poetry run python manage.py runserver
poetry run pytest
# Open a shell in the virtual environment
poetry shell
# Update dependencies
poetry update
# Export to requirements.txt (for compatibility)
poetry export -f requirements.txt --output requirements.txt
# Build and publish a package
poetry build
poetry publish
# Show dependency tree
poetry show --tree
# pyproject.toml (auto-generated)
# [tool.poetry]
# name = "my-project"
# version = "0.1.0"
# description = ""
# [tool.poetry.dependencies]
# python = "^>=3.11"
# django = "^>=5.0"
# [tool.poetry.group.dev.dependencies]
# pytest = "^>=8.0"conda: Potenza per la data science
conda è un gestore di pacchetti completo che può installare dipendenze non Python.
# conda — Data Science / ML (Anaconda/Miniconda)
# Install Miniconda (minimal)
# https://docs.conda.io/projects/miniconda/
# Create environment with specific Python version
conda create -n myproject python=3.11
# Activate environment
conda activate myproject
# Install packages (conda packages first)
conda install numpy pandas scikit-learn matplotlib
# Install packages not in conda
pip install some-pytorch-extension
# Export environment
conda env export > environment.yml
# Create from environment.yml
conda env create -f environment.yml
# List environments
conda env list
# Deactivate
conda deactivate
# Remove environment
conda env remove -n myproject
# Update conda
conda update conda
# environment.yml example:
# name: myproject
# channels:
# - conda-forge
# - defaults
# dependencies:
# - python=3.11
# - numpy=1.26
# - pandas=2.1
# - pip:
# - custom-package==1.0Confronto degli strumenti
La scelta dello strumento giusto dipende dal caso d'uso.
Tool Installation Lockfile Non-Python Build/Publish Best For
------------------------------------------------------------------------
venv Built-in No No No Simple scripts, learning
pipenv pip install Yes No No Legacy projects
Poetry curl install Yes No Yes General apps, libraries
conda Installer Yes Yes No Data science, ML, AI
uv cargo/pip Yes No Yes Fast pip replacement (2026)Domande frequenti
Quale strumento usare nel 2026?
Per lo sviluppo Python generale usa Poetry. Per data science usa conda.
Differenza tra pip e conda?
pip installa solo da PyPI. conda può installare anche pacchetti non Python.
Posso usare pip in un ambiente conda?
Sì, ma con cautela. Mescolare pip e conda può causare conflitti.
Come condividere l'ambiente?
Con Poetry: pyproject.toml e poetry.lock. Con pip: requirements.txt. Con conda: environment.yml.