Package: setuptools-rust
Setuptools plugin for Rust extensionssetuptools-rustis a plugin forsetuptoolsto build Rust Python extensions implemented withPyO3orrust-cpython.Compile and distribute Python extensions written in Rust as easily as if
they were written in C.QuickstartThe following is a very basic tutorial that shows how to usesetuptools-rustinpyproject.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 theexamples/hello-worlddirectory in thegithub repository.
ThePyO3 docshave detailed information on how to write Python
modules in Rust.hello-world
├── python
│   └── hello_world
│       └── __init__.py
└── rust
    └── lib.rsOnce the implementation files are in place, we need to add apyproject.tomlfile that tells anyone that wants to use your project how to build it.
In this file, we use anarray 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/modulesfind={where=["python"]}[[tool.setuptools-rust.ext-modules]]# Private Rust extension module to be nested into the Python packagetarget="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 omittedbinding="PyO3"# Default value, can be omittedEach extension module should map directly into the corresponding[lib]table on theCargo manifest file:# Cargo.toml[package]name="hello-world"version="0.1.0"edition="2021"[dependencies]pyo3="0.20.3"[lib]name="_lib"# private module to be nested into Python package,# needs to match the name of the function with the `[#pymodule]` attributepath="rust/lib.rs"crate-type=["cdylib"]# required for shared library for Python to import from.# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html# See also PyO3 docs on writing Cargo.toml files at https://pyo3.rsYou will also need to tell Setuptools that the Rust files are required to build your
project from thesource distribution.
That can be done either viaMANIFEST.in(see example below) or via a plugin likesetuptools-scm.# MANIFEST.in
include Cargo.toml
recursive-include rust *.rsWith these files in place, you can install the project in a virtual environment
for testing and making sure everything is working correctly:# cd hello-worldpython3-mvenv.venvsource.venv/bin/activate# on Linux or macOS.venv\Scripts\activate# on Windowspython-mpipinstall-e.python>>>importhello_world# ... try running something from your new extension module ...# ... better write some tests with pytest ...Next steps and final remarksWhen you are ready to distribute your project, have a look onthe notes in the documentation about building wheels.Cross-compiling is also supported, using one ofcrossenv,crossorcargo-zigbuild.
For examples see thetest-crossenvandtest-crossandtest-zigbuildGithub actions jobs inci.yml.You can also use[[tool.setuptools-rust.bins]](instead of[[tool.setuptools-rust.ext-modules]]),
if you want to distribute a binary executable written in Rust (instead of a library that can be imported by the Python runtime).
Note however that distributing both library and executable (or multiple executables),
may significantly increase the size of thewheelfile distributed by thepackage indexand therefore increase build, download and installation times.
Another approach is to use a Python entry-point that calls the Rust
implementation (exposed via PyO3 bindings).
See thehello-worldexample for more insights.For a complete reference of the configuration options, see theAPI reference.
You can use any parameter defined by theRustExtensionclass with[[tool.setuptools-rust.ext-modules]]and any parameter defined by theRustBinclass with[[tool.setuptools-rust.bins]]; just remember to replace
underscore characters_with dashes-in yourpyproject.tomlfile.Cargo.tomlallow only one[lib]table per file.
If you require multiple extension modules you will need to write multipleCargo.tomlfiles.
Alternatively you can create a single private Rust top-level module that exposes
multiple submodules (usingPyO3's submodules),
which may also reduce the size of the build artifacts.
You can always keep your extension modules private and wrap them in pure Python
to have fine control over the public API.If want to include both[[tool.setuptools-rust.bins]]and[[tool.setuptools-rust.ext-modules]]in the same macOS wheel, you might have to manually add an extrabuild.rsfile,
seePyO3/setuptools-rust#351for more information about the workaround.For more examples, see:hello-world:
a more complete version of the code used in this tutorial that mixes both[[tool.setuptools-rust.ext-modules]]and[[tool.setuptools-rust.bins]]in a single distribution.html-py-ever:
a more advanced example that uses Rust crates as dependencies.rust_with_cffi:
uses both Rust andCFFI.namespace_package:
integrates Rust-written modules into PEP 420 namespace packages.hello-world-script:
uses Rust only for creating binary executables, not library modules.