Moderne Python-Verpackung

Erstellen und verteilen Sie Python-Pakete mit pyproject.toml und modernen Tools

Moderne Python-Verpackung

Die Python-Paketlandschaft hat sich weiterentwickelt. Hier ist der moderne Ansatz mit pyproject.toml.

Projektstruktur

my_package/
├── pyproject.toml
├── README.md
├── src/
│   └── my_package/
│       ├── __init__.py
│       └── core.py
└── tests/
    └── test_core.py

pyproject.thumb

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "my-package"
version = "0.1.0"
description = "A useful package"
readme = "README.md"
requires-python = ">=3.11"
license = {text = "MIT"}
authors = [
    {name = "Your Name", email = "you@example.com"}
]
dependencies = [
    "requests>=2.28",
    "pydantic>=2.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=7.0",
    "mypy>=1.0",
    "ruff>=0.1",
]

[project.scripts]
my-cli = "my_package.cli:main"

Erstellen und Veröffentlichen

# Install build tools
pip install build twine

# Build package
python -m build

# Upload to PyPI
twine upload dist/*

Entwicklungsworkflow

# Create virtual environment
python -m venv .venv
source .venv/bin/activate

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Type check
mypy src/

# Lint
ruff check src/

Werkzeugvergleich

Werkzeuge Zweck
hatch Umgebungen erstellen, veröffentlichen und verwalten
poetry Abhängigkeitsmanagement + Aufbau
pdm Lokale PEP 582-Pakete
flit Einfache Veröffentlichung