Skip to main content

A Hatchling build hook plugin for building PyO3 Rust extensions

Project description

Hatchling PyO3 Plugin

A Hatchling build hook plugin for building PyO3 Rust extensions

build codecov PyPI version


Documentation: https://frankie567.github.io/hatchling-pyo3-plugin/

Source Code: https://github.com/frankie567/hatchling-pyo3-plugin


Overview

PyO3 is a popular framework for creating Python bindings to Rust code. While Maturin is the recommended build backend for PyO3 projects, this plugin enables PyO3 extensions to be built using Hatchling as an alternative build backend.

This is inspired by setuptools-rust, which provides similar functionality for setuptools.

Features

  • ✅ Build PyO3 Rust extensions as part of the Hatchling build process
  • ✅ Automatic detection of Cargo.toml
  • ✅ Cross-platform support (Linux, macOS, Windows)
  • ✅ Configurable build profiles (release/debug)
  • ✅ Custom Cargo arguments support
  • ✅ Integration with standard Python packaging workflow

Usage

Installation

Add the plugin to your pyproject.toml:

[build-system]
requires = ["hatchling", "hatchling-pyo3-plugin"]
build-backend = "hatchling.build"

[tool.hatch.build.hooks.pyo3]
# Plugin will automatically detect and build Rust extensions

Project Structure

my-project/
├── pyproject.toml
├── Cargo.toml          # Rust project configuration
├── src/
│   └── lib.rs          # Rust source code with PyO3 bindings
└── my_package/
    └── __init__.py     # Python package

Example pyproject.toml

[build-system]
requires = ["hatchling", "hatchling-pyo3-plugin"]
build-backend = "hatchling.build"

[project]
name = "my-pyo3-project"
version = "0.1.0"
description = "A project with PyO3 extensions"
requires-python = ">=3.8"

[tool.hatch.build.hooks.pyo3]
# Optional: specify Rust extensions explicitly
# If not specified, plugin will look for Cargo.toml in project root

# Optional configuration:
# cargo-manifest = "Cargo.toml"  # Path to Cargo.toml (default: "Cargo.toml")
# profile = "release"            # Build profile (default: "release", can be "debug")
# target-dir = "target"          # Cargo target directory (default: "target")
# cargo-args = ["--features", "special"]  # Additional cargo arguments

Development

Setup environment

We use uv to manage the development environment and production build, and just to manage command shortcuts. Ensure they are installed on your system.

Run unit tests

You can run all the tests with:

just test

Format the code

Execute the following command to apply linting and check typing:

just lint

Publish a new version

You can bump the version, create a commit and associated tag with one command:

just version patch
just version minor
just version major

Your default Git text editor will open so you can add information about the release.

When you push the tag on GitHub, the workflow will automatically publish it on PyPi and a GitHub release will be created as draft.

Serve the documentation

You can serve the Mkdocs documentation with:

just docs-serve

It'll automatically watch for changes in your code.

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hatchling_pyo3_plugin-0.1.0.tar.gz (62.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hatchling_pyo3_plugin-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file hatchling_pyo3_plugin-0.1.0.tar.gz.

File metadata

File hashes

Hashes for hatchling_pyo3_plugin-0.1.0.tar.gz
Algorithm Hash digest
SHA256 501dac09ba62042556dc20764f25274196ea88e6ef6b2665fb833cea3fa5c0ac
MD5 cf0ed65791943830aede41155959c375
BLAKE2b-256 c1752420cc55a2e65e0018ad105f210b027bfe04f4dd2a628f0a284c26af4a9f

See more details on using hashes here.

File details

Details for the file hatchling_pyo3_plugin-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hatchling_pyo3_plugin-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d90afb78ae8469abee30cc0a38c4aa5663c754ea18060cbd8ca2d7d7cb901674
MD5 6bb44866531f30fd32b04712dd74c590
BLAKE2b-256 edd392720141d366d588df1eae8304c6a25d09b3d8ee3b4e9a23653f9ff90ecc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page