Setuptools Rust extension plugin
npx @tessl/cli install tessl/pypi-setuptools-rust@1.12.0A setuptools plugin that provides seamless integration of Rust code into Python projects. It enables developers to build Python extensions written in Rust using PyO3 or rust-cpython bindings, compiling and distributing them as easily as C extensions.
pip install setuptools-rustfrom setuptools_rust import RustExtension, RustBin, Binding, StripFor distutils commands:
from setuptools_rust import build_rust, clean_rustfrom setuptools import setup
from setuptools_rust import RustExtension
setup(
name="my-rust-extension",
version="0.1.0",
rust_extensions=[
RustExtension(
"my_extension.rust_module", # Python module name
path="Cargo.toml", # Path to Cargo.toml
binding=Binding.PyO3, # Binding type
)
],
zip_safe=False, # Rust extensions are not zip safe
)[build-system]
requires = ["setuptools", "setuptools-rust"]
[[tool.setuptools-rust.ext-modules]]
target = "my_extension.rust_module"
path = "Cargo.toml"
binding = "PyO3"
[[tool.setuptools-rust.bins]]
target = "my-binary"
path = "Cargo.toml"setuptools-rust integrates with the Python packaging ecosystem through multiple layers:
RustExtension and RustBin classes define build targets and optionsbuild_rust and clean_rust commands handle the Cargo build processConfigure and build Rust extensions as Python modules or standalone binaries. Supports multiple binding types (PyO3, rust-cpython, custom), cross-compilation, and extensive build customization.
class RustExtension:
def __init__(
self,
target: Union[str, Dict[str, str]],
path: str = "Cargo.toml",
binding: Binding = Binding.PyO3,
**kwargs
): ...
class RustBin(RustExtension):
def __init__(
self,
target: Union[str, Dict[str, str]],
path: str = "Cargo.toml",
**kwargs
): ...
class Binding(IntEnum):
PyO3 = auto()
RustCPython = auto()
NoBinding = auto()
Exec = auto()
class Strip(IntEnum):
No = auto()
Debug = auto()
All = auto()Distutils commands for building and cleaning Rust extensions, with integration into standard Python packaging workflows.
class build_rust(RustCommand):
description = "build Rust extensions (compile/link to build directory)"
def run_for_extension(self, ext: RustExtension) -> None: ...
def build_extension(
self,
ext: RustExtension,
forced_target_triple: Optional[str] = None
) -> List[_BuiltModule]: ...
class clean_rust(RustCommand):
description = "clean Rust extensions"
def run_for_extension(self, ext: RustExtension) -> None: ...Comprehensive configuration through pyproject.toml files, supporting extension modules and binaries with full control over build options, features, and environment variables.
[[tool.setuptools-rust.ext-modules]]
target = "module_name"
path = "Cargo.toml"
binding = "PyO3"
features = ["feature1", "feature2"]
debug = false
[[tool.setuptools-rust.bins]]
target = "binary_name"
path = "Cargo.toml"
strip = "Debug"from typing import Union, Dict, List, Optional, Sequence, Literal
from enum import IntEnum, auto
CargoMetadata = NewType("CargoMetadata", Dict[str, Any])
class Env:
"""Hashable wrapper for environment variable dictionaries."""
class SimpleSpec:
"""Parsed Rust version requirement specification."""Key environment variables that control the build process:
SETUPTOOLS_RUST_CARGO_PROFILE: Override cargo profile (e.g., "release", "dev")CARGO_BUILD_TARGET: Set build target triple for cross-compilationCARGO: Path to cargo executableRUSTFLAGS: Additional flags passed to rustc compiler