Zum Inhalt

_

_

Poetry Cheatsheet

• Installation

Platform Command
Linux/macOS (Official) INLINE_CODE_8
macOS (Homebrew) INLINE_CODE_9
Windows (PowerShell) INLINE_CODE_10
Any Platform (pipx) INLINE_CODE_11
Verify Installation INLINE_CODE_12
Update Poetry INLINE_CODE_13
Update to Specific Version INLINE_CODE_14
_
Add to PATH (falls offizieller Installateur):
export PATH="$HOME/.local/bin:$PATH"
# Add to ~/.bashrc or ~/.zshrc for persistence
```_

oder Grundlegende Befehle

| Command | Description |
|---------|-------------|
| __INLINE_CODE_15__ | Create new project with standard structure |
| __INLINE_CODE_16__ | Create project with src layout |
| __INLINE_CODE_17__ | Initialize Poetry in existing project (interactive) |
| __INLINE_CODE_18__ | Install all dependencies from lock file |
| __INLINE_CODE_19__ | Add package to dependencies |
| __INLINE_CODE_20__ | Add package to dev dependencies |
| __INLINE_CODE_21__ | Remove package from dependencies |
| __INLINE_CODE_22__ | Update all dependencies to latest allowed versions |
| __INLINE_CODE_23__ | Update specific package |
| __INLINE_CODE_24__ | List all installed packages |
| __INLINE_CODE_25__ | Show dependency tree |
| __INLINE_CODE_26__ | Show details of specific package |
| __INLINE_CODE_27__ | Generate/update poetry.lock file |
| __INLINE_CODE_28__ | Validate pyproject.toml and poetry.lock |
| __INLINE_CODE_29__ | Run command in virtual environment |
| __INLINE_CODE_30__ | Run tests in virtual environment |
| __INLINE_CODE_31__ | Activate virtual environment |
| __INLINE_CODE_32__ | Show virtual environment information |
| __INLINE_CODE_33__ | List all virtual environments |
| __INLINE_CODE_34__ | Build source and wheel distributions |
_
/ Fortgeschrittene Nutzung

| Command | Description |
|---------|-------------|
| __INLINE_CODE_35__ | Add package with caret version constraint (>=2.28.0 __HTML_TAG_130__=2.28.0 __HTML_TAG_131__=2.28.0 __HTML_TAG_132__=2.28.0 <2.29.0 (patch updates) |
| Exact | __INLINE_CODE_74__ | Exactly version 2.28.0 |
| Greater/Equal | __INLINE_CODE_75__ | Version 2.28.0 or higher |
| Range | __INLINE_CODE_76__ | Between 2.28 and 3.0 |
| Wildcard | __INLINE_CODE_77__ | Any patch version of 2.28 |
| Latest | __INLINE_CODE_78__ | Latest available version |
_
Häufige Anwendungsfälle

### Use Case 1: Neues Python-Projekt starten

```bash
# Create new project with src layout
poetry new my-app --src

# Navigate to project
cd my-app

# Add production dependencies
poetry add fastapi uvicorn pydantic-settings

# Add development dependencies
poetry add pytest pytest-cov black mypy --group dev

# Install all dependencies
poetry install

# Activate virtual environment
poetry shell

Use Case 2: Konvertieren des vorhandenen Projekts in Poesie

# Navigate to existing project
cd existing-project

# Initialize Poetry (interactive)
poetry init

# Add existing dependencies
poetry add requests flask sqlalchemy

# Add dev dependencies from requirements-dev.txt
poetry add pytest black flake8 --group dev

# Generate lock file
poetry lock

# Install everything
poetry install

Use Case 3: Mehrere Abhängigkeitsgruppen verwalten

# Install with documentation dependencies
poetry install --with docs

# Install without test dependencies
poetry install --without test

# Install only main dependencies (no dev/test)
poetry install --only main

# Add dependencies to specific groups
poetry add sphinx sphinx-rtd-theme --group docs
poetry add locust --group perf

Use Case 4: Erstellen und Veröffentlichen eines Pakets

# Update version in pyproject.toml
poetry version patch  # 0.1.0 -> 0.1.1
poetry version minor  # 0.1.1 -> 0.2.0
poetry version major  # 0.2.0 -> 1.0.0

# Build package
poetry build

# Test publish to TestPyPI
poetry config repositories.testpypi https://test.pypi.org/legacy/
poetry publish -r testpypi --username __token__ --password $TEST_PYPI_TOKEN

# Publish to PyPI
poetry publish --username __token__ --password $PYPI_TOKEN

Use Case 5: Arbeiten mit privaten Paket Repositories

# Add private repository
poetry source add --priority=supplemental private-repo https://pypi.company.com/simple/

# Configure authentication
poetry config http-basic.private-repo username password

# Or use token authentication
poetry config pypi-token.private-repo your-token

# Add package from private repo
poetry add internal-package

# Publish to private repo
poetry publish -r private-repo

Use Case 6: CI/CD Integration

# Install Poetry in CI
curl -sSL https://install.python-poetry.org | python3 -

# Install dependencies (no dev dependencies)
poetry install --without dev --no-interaction --no-ansi

# Export for Docker or other tools
poetry export -f requirements.txt --output requirements.txt --without-hashes

# Run tests
poetry run pytest

# Build package
poetry build

oder Best Practices

Commit __INLINE_CODE_79_*: Beauftragen Sie immer die Sperrdatei zur Versionskontrolle, um reproduzierbare Builds in allen Umgebungen und Teammitgliedern sicherzustellen.

**Benutze Abhängigkeitsgruppen*: Organisiere Abhängigkeiten in logische Gruppen (dev, test, docs) anstelle der deprecated --dev___-Flag für bessere Kontrolle und Flexibilität.

  • Pin Python version: Geben Sie die Python-Version in pyproject.toml_ an, um Kompatibilitätsprobleme in verschiedenen Umgebungen zu verhindern.

  • **Benutzen poetry.lock für Anwendungen*: Für Anwendungen und Dienstleistungen verlassen Sie sich auf die Schlossdatei für genaue Versionen. Testen Sie für Bibliotheken die in pyproject.toml_ angegebenen Versionen.

  • **Prefer poetry install --sync*: In CI/CD-Pipelines verwenden Sie --sync, um sicherzustellen, dass die Umgebung genau mit der Schlossdatei übereinstimmt, indem Sie zusätzliche Pakete entfernen.

  • **Set virtualenvs.in-project*: Konfigurieren Sie Poetry, um virtuelle Umgebungen im Projektverzeichnis (poetry config virtualenvs.in-project true) für eine einfachere IDE-Integration und -Reinigung zu erstellen.

  • **Diese Versionszwänge sinnvoll nutzen*: Verwenden Sie Caret (^) für die meisten Abhängigkeiten, um kompatible Updates zu ermöglichen, aber verwenden Sie genaue Versionen für Pakete mit instabilen APIs oder brechen Änderungen.

  • **Export für Kompatibilität*: Generieren requirements.txt Dateien, wenn sie für Werkzeuge benötigt werden, die nicht unterstützen Poesie, vor allem in Docker-Containern oder Legacy-Systemen.

  • **Keep Poetry aktualisiert*: Regelmäßig aktualisieren Sie Poetry selbst (poetry self update), um von Bugfixes, Leistungsverbesserungen und neuen Features zu profitieren.

  • **Benutzen __INLINE_CODE_94_ vor Commits*: Validieren Sie Ihre __INLINE_CODE_95_ und poetry.lock-Dateien, bevor Sie Konfigurationsfehler frühzeitig erfassen.

Fehlerbehebung

Issue Solution
INLINE_CODE_97 Add Poetry to PATH: INLINE_CODE_98 or restart terminal after installation
Virtual environment not activating Use INLINE_CODE_99 or run commands with INLINE_CODE_100. Check INLINE_CODE_101 to verify environment exists
Dependency resolution takes too long Clear cache with INLINE_CODE_102, update Poetry, or try INLINE_CODE_103
Lock file out of sync errors Run INLINE_CODE_104 to regenerate lock file, or INLINE_CODE_105 to match environment to lock
Package not found in private repo Verify source configuration with INLINE_CODE_106, check authentication with INLINE_CODE_107
INLINE_CODE_108 during install Check for conflicting version constraints in INLINE_CODE_109, try updating dependencies with INLINE_CODE_110
Poetry using wrong Python version Specify Python explicitly: INLINE_CODE_111 or INLINE_CODE_112
Cannot publish to PyPI (authentication) Configure token: INLINE_CODE_113 or use INLINE_CODE_114 and INLINE_CODE_115 flags
INLINE_CODE_116 merge conflicts Run INLINE_CODE_117 after resolving conflicts in INLINE_CODE_118 to regenerate lock file
Slow package installation Enable parallel installation: INLINE_CODE_119 and adjust workers if needed
IDE not detecting virtual environment Use INLINE_CODE_120 and point IDE to INLINE_CODE_121 directory in project root
_ Überholte Pakete, die Probleme verursachen