Using Python Virtual Environments the Right Way
Why virtual environments matter and how to use them effectively.
The problem
Every Python developer knows the problem: project A needs Django 4.2, project B still needs Django 3.2. Without isolation this ends in chaos.
The solution: virtual environments
# Erstellen einer virtuellen Umgebung
$ python -m venv .venv
# Aktivieren
$ source .venv/bin/activate
# Pakete installieren
(.venv) $ pip install django==4.2
UV instead of pip
My tip: use uv instead of pip. It is 10-100x faster:
# Installation
$ pip install uv
# Pakete installieren
$ uv pip install -r requirements.txt
Best Practices
- One environment per project - no global installations
- Maintain requirements.txt - always with versions
- .venv in .gitignore - never commit it to the repository
- Automate activation - e.g. with direnv
Clean environments = clean code!