Modern, extensible Python build backend implementing PEP 517/660 standards with plugin architecture.
—
Core build backend functions implementing Python packaging standards (PEP 517 and PEP 660) for integration with build frontends like pip, build, and other packaging tools.
Creates source distributions (sdist) containing all source files needed to build the project.
def build_sdist(sdist_directory: str, config_settings: dict[str, Any] | None = None) -> str:
"""
Build a source distribution.
Args:
sdist_directory: Directory where the sdist will be written
config_settings: Optional configuration settings from build frontend
Returns:
str: Basename of the built sdist file
PEP Reference:
https://peps.python.org/pep-0517/#build-sdist
"""Creates wheel distributions (.whl files) containing compiled bytecode and data files for installation.
def build_wheel(
wheel_directory: str,
config_settings: dict[str, Any] | None = None,
metadata_directory: str | None = None
) -> str:
"""
Build a wheel distribution.
Args:
wheel_directory: Directory where the wheel will be written
config_settings: Optional configuration settings from build frontend
metadata_directory: Optional directory containing prepared metadata
Returns:
str: Basename of the built wheel file
PEP Reference:
https://peps.python.org/pep-0517/#build-wheel
"""Creates editable wheels for development installations that link to source code rather than copying files.
def build_editable(
wheel_directory: str,
config_settings: dict[str, Any] | None = None,
metadata_directory: str | None = None
) -> str:
"""
Build an editable wheel distribution.
Args:
wheel_directory: Directory where the editable wheel will be written
config_settings: Optional configuration settings from build frontend
metadata_directory: Optional directory containing prepared metadata
Returns:
str: Basename of the built editable wheel file
PEP Reference:
https://peps.python.org/pep-0660/#build-editable
"""Functions to determine build-time dependencies before building distributions.
def get_requires_for_build_sdist(config_settings: dict[str, Any] | None = None) -> list[str]:
"""
Get build dependencies for source distribution.
Args:
config_settings: Optional configuration settings from build frontend
Returns:
list[str]: List of build dependency specifications
PEP Reference:
https://peps.python.org/pep-0517/#get-requires-for-build-sdist
"""
def get_requires_for_build_wheel(config_settings: dict[str, Any] | None = None) -> list[str]:
"""
Get build dependencies for wheel distribution.
Args:
config_settings: Optional configuration settings from build frontend
Returns:
list[str]: List of build dependency specifications
PEP Reference:
https://peps.python.org/pep-0517/#get-requires-for-build-wheel
"""
def get_requires_for_build_editable(config_settings: dict[str, Any] | None = None) -> list[str]:
"""
Get build dependencies for editable wheel distribution.
Args:
config_settings: Optional configuration settings from build frontend
Returns:
list[str]: List of build dependency specifications (includes editables requirement)
PEP Reference:
https://peps.python.org/pep-0660/#get-requires-for-build-editable
"""These functions are only available when not running under pip to avoid dependency resolution issues.
def prepare_metadata_for_build_wheel(
metadata_directory: str,
config_settings: dict[str, Any] | None = None
) -> str:
"""
Prepare wheel metadata without building the full wheel.
Args:
metadata_directory: Directory where metadata will be written
config_settings: Optional configuration settings from build frontend
Returns:
str: Basename of the metadata directory
PEP Reference:
https://peps.python.org/pep-0517/#prepare-metadata-for-build-wheel
Note:
Only available when PIP_BUILD_TRACKER environment variable is not set
"""
def prepare_metadata_for_build_editable(
metadata_directory: str,
config_settings: dict[str, Any] | None = None
) -> str:
"""
Prepare editable wheel metadata without building the full wheel.
Args:
metadata_directory: Directory where metadata will be written
config_settings: Optional configuration settings from build frontend
Returns:
str: Basename of the metadata directory
PEP Reference:
https://peps.python.org/pep-0660/#prepare-metadata-for-build-editable
Note:
Only available when PIP_BUILD_TRACKER environment variable is not set
"""# pyproject.toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"import os
from hatchling.build import build_wheel, build_sdist, get_requires_for_build_wheel
# Get build dependencies
deps = get_requires_for_build_wheel()
print(f"Build dependencies: {deps}")
# Build a wheel
wheel_name = build_wheel("dist")
print(f"Built wheel: {wheel_name}")
# Build a source distribution
sdist_name = build_sdist("dist")
print(f"Built sdist: {sdist_name}")# Configuration settings can be passed by build frontends
config_settings = {
"build-dir": "custom_build",
"skip-binary-deps": "true"
}
wheel_name = build_wheel("dist", config_settings=config_settings)Build backend functions may raise various exceptions:
ValueError: Invalid configuration or settingsFileNotFoundError: Missing required files or directoriesPermissionError: Insufficient permissions for file operationsRuntimeError: Build failures or internal errorsBuild frontends should handle these exceptions appropriately and provide meaningful error messages to users.
Hatchling integrates with build frontends through the standardized PEP 517/660 interface:
get_requires_for_build_* to determine dependenciesbuild_* functions to create distributionsprepare_metadata_for_build_* for metadata-only operationsHatchling includes a special ouroboros.py module for self-building that provides the same PEP 517/660 interface but with minimal dependencies for bootstrapping the build process.
Install with Tessl CLI
npx tessl i tessl/pypi-hatchling