Setuptools Rust extension plugin
Maintained. Last release 5 months ago, commits in 7 of the last 26 weeks. Weaker on release cadence and contributor spread.
pip install setuptools-rustSetuptools plugin for Rust extensions
setuptools-rust is a plugin for setuptools to build Rust Python extensions implemented with PyO3 or rust-cpython.
Compile and distribute Python extensions written in Rust as easily as if they were written in C.
Quickstart
The following is a very basic tutorial that shows how to use setuptools-rust in pyproject.toml.
It assumes that you already have a bunch of Python and Rust files that you want
to distribute. You can see examples for these files in the
examples/hello-world
directory in the github repository.
The PyO3 docs have detailed information on how to write Python
modules in Rust.
hello-world
├── python
│ └── hello_world
│ └── __init__.py
└── rust
└── lib.rs
Once the implementation files are in place, we need to add a pyproject.toml
file that tells anyone that wants to use your project how to build it.
In this file, we use an array of tables
(TOML jargon equivalent to Python's list of dicts) for [[tool.setuptools-rust.ext-modules]],
to specify different extension modules written in Rust:
# pyproject.toml
[build-system]
requires = ["setuptools", "setuptools-rust"]
build-backend = "setuptools.build_meta"
[project]
name = "hello-world"
version = "1.0"
[tool.setuptools.packages]
# Pure Python packages/modules
find = { where = ["python"] }
[[tool.setuptools-rust.ext-modules]]
# Private Rust extension module to be nested into the Python package
target = "hello_world._lib" # The last part of the name (e.g. "_lib") has to match lib.name in Cargo.toml,
# but you can add a prefix to nest it inside of a Python package.
path = "Cargo.toml" # Default value, can be omitted
binding = "PyO3" # Default value, can be omitted