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.1.tar.gz (62.6 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.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for hatchling_pyo3_plugin-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2c7de4cb025ac81b32bcd74d3961bf1fd33d0132d5ef7de5f180aac93465841e
MD5 caa6236faf6cf106d410f050b705c81d
BLAKE2b-256 19b0ef968c789bf8f51ec867a18005faa8a8333b35352a510e82efdb2e40df65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hatchling_pyo3_plugin-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 16970dba0a0dc3331ca615b0790b384a4cad65b320f05e82dcbbca95938e3cc6
MD5 143c6975de2627e9623a831438ea98d4
BLAKE2b-256 d5e696003e28108be319a571dcf4db5db3dea7a7eb21fcb21f65eb759b6413d9

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