Virtuelle Umgebungen sind der Grundstein der Python-Abhängigkeitsverwaltung. Dieser Leitfaden behandelt die vier wichtigsten Tools: venv, conda, pipenv und Poetry.
Warum virtuelle Umgebungen wichtig sind
Jedes Python-Projekt hat eigene Abhängigkeiten mit spezifischen Versionsanforderungen. Virtuelle Umgebungen lösen dies durch isolierte Python-Installationen pro Projekt.
venv: Eingebaute Standardbibliothek
venv ist in Python 3.3+ enthalten und die einfachste Option. Keine zusätzliche Installation erforderlich.
# 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: Modernes Abhängigkeitsmanagement
Poetry ist das modernste und umfangreichste Python-Abhängigkeitsverwaltungstool.
# 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: Data-Science-Powerhouse
conda ist ein vollständiger Paketmanager, der auch Nicht-Python-Abhängigkeiten installieren kann.
# 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.0Tool-Vergleich
Die Wahl des richtigen Tools hängt von Ihrem Anwendungsfall ab.
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)Häufig gestellte Fragen
Welches virtuelle Umgebungstool sollte ich 2026 verwenden?
Für allgemeine Python-Entwicklung und Web-Apps verwenden Sie Poetry. Für Data Science und ML verwenden Sie conda.
Was ist der Unterschied zwischen pip und conda?
pip installiert Python-Pakete nur von PyPI. conda kann auch Nicht-Python-Pakete installieren.
Kann ich pip innerhalb einer conda-Umgebung verwenden?
Ja, aber mit Vorsicht. Das Mischen von pip und conda kann manchmal zu Konflikten führen.
Wie teile ich meine Umgebung mit anderen Entwicklern?
Mit Poetry: pyproject.toml und poetry.lock committen. Mit pip: requirements.txt. Mit conda: environment.yml.