Hatchling is a modern, extensible build backend for Python projects that simplifies packaging, building, and distribution while providing powerful customization options.
Hatchling helps manage Python project builds by:
- Providing a modern, standards-compliant build system
- Supporting dynamic version management
- Offering a plugin system for customization
- Enabling metadata customization
- Supporting various project structures
- Integrating with the Python packaging ecosystem
- Working seamlessly with other tools like pip, uv, and PyPI
Hatchling is included as a development dependency:
# Install with other development dependencies
uv sync --devTo install it directly:
uv pip install hatchlingIn this project, Hatchling is used to:
- Define the project's build system
- Manage package metadata
- Handle version information
- Configure package discovery and inclusion
- Support the build and distribution process
Hatchling is configured in the pyproject.toml file:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "your-package-name"
version = "0.1.0"
description = "Your package description"
readme = "README.md"
requires-python = ">=3.11"
license = { text = "MIT" }
authors = [
{ name = "Your Name", email = "your.email@example.com" },
]
dependencies = [
# List of dependencies
]
[tool.hatch.build.targets.wheel]
packages = ["src/your_package"]To build a package using Hatchling:
# Build a wheel
uv run hatch build
# Build a source distribution
uv run hatch build --target sdist
# Build both wheel and sdist
uv run hatch build --target wheel --target sdist# Build and publish to PyPI
uv run hatch publish
# Build and publish to TestPyPI
uv run hatch publish --repo test[tool.hatch.version]
path = "src/your_package/__about__.py"With __about__.py containing:
__version__ = "0.1.0"[tool.hatch.build.targets.wheel]
packages = ["src/your_package"][project.optional-dependencies]
dev = [
"pytest>=7.0.0",
"pytest-cov>=4.1.0",
]
docs = [
"mkdocs>=1.4.0",
"mkdocs-material>=9.0.0",
]When you run hatch build, Hatchling performs these steps:
- Configuration Loading: Reads the
pyproject.tomlfile - Version Resolution: Determines the package version
- Metadata Preparation: Prepares package metadata
- Source Processing: Processes source files according to configuration
- Build Execution: Creates the distribution packages (wheel, sdist)
- Artifact Generation: Outputs the final distribution files
- Use src layout: Place your package code in a
srcdirectory for cleaner separation. - Specify Python version: Always set the
requires-pythonfield to ensure compatibility. - Include a README: Provide a README.md file for PyPI display.
- Manage versions properly: Use a single source of truth for version information.
- Define optional dependencies: Group dependencies logically using optional-dependencies.
- Include license information: Always specify license information in your project metadata.
- Use dynamic metadata: Take advantage of Hatchling's dynamic metadata capabilities for complex projects.
[project]
dependencies = [
"importlib-metadata>=4.6; python_version < '3.10'",
"tomli>=2.0.0; python_version < '3.11'",
][tool.hatch.build.hooks.custom]
path = "build_hooks.py"With a custom hook in build_hooks.py:
def hook(version, build_data, artifacts):
# Custom build logic here
return artifacts