← Zurück zum Blog

$ cat blog/python-virtual-environments-25.12.2024.md

#python #venv #development

Das Problem

Jeder Python-Entwickler kennt das Problem: Projekt A braucht Django 4.2, Projekt B noch Django 3.2. Ohne Isolation endet das im Chaos.

Die Lösung: Virtual Environments

# Erstellen einer virtuellen Umgebung
$ python -m venv .venv

# Aktivieren
$ source .venv/bin/activate

# Pakete installieren
(.venv) $ pip install django==4.2

UV statt pip

Mein Tipp: Nutze uv statt pip. Es ist 10-100x schneller:

# Installation
$ pip install uv

# Pakete installieren
$ uv pip install -r requirements.txt

Best Practices

  1. Eine Umgebung pro Projekt - Keine globalen Installationen
  2. requirements.txt pflegen - Immer mit Versionen
  3. .venv in .gitignore - Nie ins Repository committen
  4. Aktivierung automatisieren - z.B. mit direnv

Saubere Umgebungen = sauberer Code!

?
v1.0.0