freshcrate
Home > Frameworks > setuptools-rust

setuptools-rust

Setuptools Rust extension plugin

Description

# Setuptools plugin for Rust extensions [![github actions](https://github.com/PyO3/setuptools-rust/actions/workflows/ci.yml/badge.svg)](https://github.com/PyO3/setuptools-rust/actions/workflows/ci.yml) [![pypi package](https://badge.fury.io/py/setuptools-rust.svg)](https://pypi.org/project/setuptools-rust/) [![readthedocs](https://readthedocs.org/projects/pip/badge/)](https://setuptools-rust.readthedocs.io/en/latest/) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) `setuptools-rust` is a plugin for `setuptools` to build Rust Python extensions implemented with [PyO3](https://github.com/PyO3/pyo3) or [rust-cpython](https://github.com/dgrunwald/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`](https://github.com/PyO3/setuptools-rust/tree/main/examples/hello-world) directory in the [github repository](https://github.com/PyO3/setuptools-rust). The [PyO3 docs](https://pyo3.rs) 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](https://toml.io/en/v1.0.0#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: ```toml # 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 ``` Each extension module should map directly into the corresponding `[lib]` table on the [Cargo manifest file](https://doc.rust-lang.org/cargo/reference/manifest.html): ```toml # Cargo.toml [package] name = "hello-world" version = "0.1.0" edition = "2021" [dependencies] pyo3 = "0.25" [lib] name = "_lib" # private module to be nested into Python package, # needs to match the name of the function with the `[#pymodule]` attribute path = "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.rs ``` You will also need to tell Setuptools that the Rust files are required to build your project from the [source distribution](https://setuptools.pypa.io/en/latest/userguide/miscellaneous.html). That can be done either via `MANIFEST.in` (see example below) or via a plugin like [`setuptools-scm`](https://pypi.org/project/setuptools-scm/). ``` # MANIFEST.in include Cargo.toml recursive-include rust *.rs ``` With these files in place, you can install the project in a virtual environment for testing and making sure everything is working correctly: ```powershell # cd hello-world python3 -m venv .venv source .venv/bin/activate # on Linux or macOS .venv\Scripts\activate # on Windows python -m pip install -e . python >>> import hello_world # ... try running something from your new extension module ... # ... better write some tests with pytest ... ``` ## Environment variables for configuration As well as all [environment variables supported by Cargo](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads), `setuptools-rust` also supports the following: - `SETUPTOOLS_RUST_CARGO_PROFILE`: used to override the profile of the Rust build. Defaults to `release`, e.g. set to `dev` to do a debug build. ## Next steps and final remarks - When you are ready to distribute your project, have a look on [the notes in the documentation about building wheels](https://setuptools-rust.readthedocs.io/en/latest/building_wheels.html). - Cross-compiling is also supported, using one of [`crossenv`](https://github.com/benfogle/crossenv), [`cross`](https://github.com/rust-embedded/cross) or [`cargo-zigbuild`](https://github.com/messen

Release History

VersionChangesUrgencyDate
1.12.1Imported from PyPI (1.12.1)Low4/21/2026
v1.12.1## What's Changed * Isolate `universal2` artifact-combination logic by @jakelishman in https://github.com/PyO3/setuptools-rust/pull/572 * Strip target suffix for cargo-zigbuild compatibility by @bet4it in https://github.com/PyO3/setuptools-rust/pull/534 * ci: configure trusted publishing by @davidhewitt in https://github.com/PyO3/setuptools-rust/pull/581 ## New Contributors * @jakelishman made their first contribution in https://github.com/PyO3/setuptools-rust/pull/572 * @bet4it made theMedium3/26/2026
v1.12.0## Added - Set PYO3_BUILD_EXTENSION_MODULE environment variable when building PyO3 extensions. https://github.com/PyO3/setuptools-rust/pull/540Low8/29/2025
v1.11.1### Fixed - Fix finding cargo artifacts when filenames are empty. [#521](https://github.com/PyO3/setuptools-rust/pull/521)Low4/4/2025
v1.11.0### Packaging - Drop support for Python 3.8. [#479](https://github.com/PyO3/setuptools-rust/pull/479) - Support free-threaded Python. [#502](https://github.com/PyO3/setuptools-rust/pull/502) - Support adding custom env vars. [#504](https://github.com/PyO3/setuptools-rust/pull/504)Low3/14/2025
v1.10.2### Fixed - Fix deprecation warning from use of `wheel.bdist_wheel`. Low10/2/2024
v1.10.1### Fixed - Fix regression in 1.10.0 where editable builds would be built in release modeLow8/4/2024
v1.10.0### Packaging - Extend macOS build flags to iOS, tvOS and watchOS. [#436](https://github.com/PyO3/setuptools-rust/pull/436) - Support Python 3.13. [#446](https://github.com/PyO3/setuptools-rust/pull/446) ### Changed - Add `SETUPTOOLS_RUST_PEP517_USE_BASE_PYTHON` environment variable to use the base interpreter path when running inside a virtual environment to avoid recompilation when switching between virtual environments. [#429](https://github.com/PyO3/setuptools-rust/pull/429) - DelayLow8/3/2024
v1.9.0### Changed - Deprecate `py_limited_api` option to `RustExtension` in favour of always using `"auto"` to configure this from `bdist_wheel`. [#410](https://github.com/PyO3/setuptools-rust/pull/410)Low2/24/2024
v1.8.1### Fixed - Fix regression in `install_extension` crashing since 1.8.0. [#380](https://github.com/PyO3/setuptools-rust/pull/380)Low10/30/2023
v1.8.0### Packaging - Drop support for Python 3.7. [#357](https://github.com/PyO3/setuptools-rust/pull/357) - Remove direct imports from `pkg_resources`. [#359](https://github.com/PyO3/setuptools-rust/pull/359) ### Added - Add support for setting a custom cargo profile with the `SETUPTOOLS_RUST_CARGO_PROFILE` environment variable. [#364](https://github.com/PyO3/setuptools-rust/pull/364)Low10/26/2023
v1.7.0### Packaging - Remove direct imports from `distutils`. [#336](https://github.com/PyO3/setuptools-rust/pull/336) - Include `py.typed` when packaging to denote that setuptools-rust includes type hints. [#338](https://github.com/PyO3/setuptools-rust/pull/338) ### Added - Add support for `pyproject.toml` configuration using `[tool.setuptools-rust]` options. [#348](https://github.com/PyO3/setuptools-rust/pull/348) ### Fixed - Fix `plat_name` handling in the case `bdist_wheel.plat_name` is Low8/22/2023
v1.6.0### Changed - Prefer passing `--crate-type` option to cargo if "toolchain >= 1.64". [#322](https://github.com/PyO3/setuptools-rust/pull/322) ### Fixed - Fix a bug where rebuilding the library would cause any running processes using it to segfault. [#295](https://github.com/PyO3/setuptools-rust/pull/295) - Fix `setup.cfg` format for compatibility with "poetry==1.4.0". [#319](https://github.com/PyO3/setuptools-rust/pull/319)Low4/27/2023
v1.5.2### Fixed - Fix regression in `dylib` build artifacts not being found since 1.5.0. [#290](https://github.com/PyO3/setuptools-rust/pull/290) - Fix regression in sdist missing examples and other supplementary files since 1.5.0. [#291](https://github.com/PyO3/setuptools-rust/pull/291)Low9/19/2022
v1.5.1### Fixed - Fix regression in `get_lib_name` crashing since 1.5.0. [#280](https://github.com/PyO3/setuptools-rust/pull/280) - Fix regression in `Binding.Exec` builds with multiple executables not finding built executables since 1.5.0. [#283](https://github.com/PyO3/setuptools-rust/pull/283)Low8/14/2022
v1.5.0### Added - Add support for extension modules built for wasm32-unknown-emscripten with Pyodide. [#244](https://github.com/PyO3/setuptools-rust/pull/244) ### Changed - Locate cdylib artifacts by handling messages from cargo instead of searching target dir (fixes build on MSYS2). [#267](https://github.com/PyO3/setuptools-rust/pull/267) - No longer guess cross-compile environment using `HOST_GNU_TYPE` / `BUILD_GNU_TYPE` sysconfig variables. [#269](https://github.com/PyO3/setuptools-rust/pull/Low8/9/2022
v1.4.1### Fixed - Fix crash when checking Rust version. [#263](https://github.com/PyO3/setuptools-rust/pull/263)Low7/5/2022
v1.4.0### Packaging - Increase minimum `setuptools` version to 62.4. [#222](https://github.com/PyO3/setuptools-rust/pull/246) ### Added - Add `cargo_manifest_args` to support locked, frozen and offline builds. [#234](https://github.com/PyO3/setuptools-rust/pull/234) - Add `RustBin` for packaging binaries in scripts data directory. [#248](https://github.com/PyO3/setuptools-rust/pull/248) ### Changed - `Exec` binding `RustExtension` with `script=True` is deprecated in favor of `RustBin`. [#248Low7/5/2022
v1.3.0### Packaging - Increase minimum `setuptools` version to 58. [#222](https://github.com/PyO3/setuptools-rust/pull/222) ### Fixed - Fix crash when `python-distutils-extra` linux package is installed. [#222](https://github.com/PyO3/setuptools-rust/pull/222) - Fix sdist built with vendored dependencies on Windows having incorrect cargo config. [#223](https://github.com/PyO3/setuptools-rust/pull/223)Low4/26/2022
v1.2.0### Packaging - Drop support for Python 3.6. [#209](https://github.com/PyO3/setuptools-rust/pull/209) ### Added - Add support for `kebab-case` executable names. [#205](https://github.com/PyO3/setuptools-rust/pull/205) - Add support for custom cargo profiles. [#216](https://github.com/PyO3/setuptools-rust/pull/216) ### Fixed - Fix building macOS arm64 wheel with cibuildwheel. [#217](https://github.com/PyO3/setuptools-rust/pull/217)Low3/22/2022
v1.1.2## 1.1.2 ### Changed - Removed dependency on `tomli` to simplify installation. [#200](https://github.com/PyO3/setuptools-rust/pull/200) - Improve error messages on invalid inputs to `rust_extensions` keyword. [#203](https://github.com/PyO3/setuptools-rust/pull/203)Low12/5/2021
v1.1.1## 1.1.1 ### Fixed - Fix regression from `setuptools-rust` 1.1.0 which broke builds for the `x86_64-unknown-linux-musl` target. [#194](https://github.com/PyO3/setuptools-rust/pull/194) - Fix `--target` command line option being unable to take a value. [#195](https://github.com/PyO3/setuptools-rust/pull/195) - Fix regression from `setuptools-rust` 1.0.0 which broke builds on arm64 macos conda builds. [#196](https://github.com/PyO3/setuptools-rust/pull/196) - Fix regression from `setuptools-rLow12/1/2021
v1.1.0## v1.1.0 ### Added - Add support for cross-compiling using [`cross`](https://github.com/rust-embedded/cross). [#185](https://github.com/PyO3/setuptools-rust/pull/185) ### Fixed - Fix incompatibility with Python 3.6.0 using default values for NamedTuple classes. [#184](https://github.com/PyO3/setuptools-rust/pull/184) - Stop forcing the `msvc` Rust toolchain for Windows environments using the `gnu` toolchain. [#187](https://github.com/PyO3/setuptools-rust/pull/187)Low11/30/2021
v1.0.0## v1.0.0 ### Added - Add `--target` command line option for specifying target triple. [#136](https://github.com/PyO3/setuptools-rust/pull/136) - Add new default `"auto"` setting for `RustExtension.py_limited_api`. [#137](https://github.com/PyO3/setuptools-rust/pull/137) - Support very verbose cargo build.rs output. [#140](https://github.com/PyO3/setuptools-rust/pull/140) ### Changed - Switch to `tomli` dependency. [#174](https://github.com/PyO3/setuptools-rust/pull/174) ### Removed Low11/21/2021
v0.12.1## v0.12.1 ### Fixed - Fix some files unexpectedly missing from `sdist` command output. [#125](https://github.com/PyO3/setuptools-rust/pull/125)Low3/11/2021
v0.12.0## v0.12.0 ### Packaging - Bump minimum Python version to Python 3.6. ### Added - Support building x86-64 wheel on arm64 macOS machine. [#114](https://github.com/PyO3/setuptools-rust/pull/114) - Add macOS universal2 wheel building support. [#115](https://github.com/PyO3/setuptools-rust/pull/115) - Add option to cargo vendor crates into sdist. [#118](https://github.com/PyO3/setuptools-rust/pull/118) ### Changed - Respect `PYO3_PYTHON` and `PYTHON_SYS_EXECUTABLE` environment variableLow3/8/2021
v0.11.6## Changed - Respect `CARGO_BUILD_TARGET` environment variable if set. [#90](https://github.com/PyO3/setuptools-rust/pull/90) - Add `setuptools_rust.__version__` and require setuptools >= 46.1. [#93](https://github.com/PyO3/setuptools-rust/pull/93)Low12/13/2020
v0.11.5## Changed - Fix support for Python 3.5. [#86](https://github.com/PyO3/setuptools-rust/pull/86) - Fix further cases of building for 32-bit Python on 64-bit Windows. [#87](https://github.com/PyO3/setuptools-rust/pull/87)Low11/10/2020
v0.11.4## Changed - Fix `tomlgen` functionality on Windows. [#78](https://github.com/PyO3/setuptools-rust/pull/78) - Add support for building abi3 shared objects. [#82](https://github.com/PyO3/setuptools-rust/pull/82)Low11/3/2020
v0.11.3## Changed - Fix building on Linux distributions that use musl (e.g. Alpine) out of the box. [#80](https://github.com/PyO3/setuptools-rust/pull/80)Low8/24/2020
v0.11.2## Changed - Fix support for namespace packages. [#79](https://github.com/PyO3/setuptools-rust/pull/79)Low8/10/2020
v0.11.1## Changed - Fix building for 32-bit Python on 64-bit Windows. [#77](https://github.com/PyO3/setuptools-rust/pull/77) Low8/7/2020
v0.11.0## Changes - Remove python 2 support. [#53](https://github.com/PyO3/setuptools-rust/pull/53) - Fix compatibility with `cffi`. [#68](https://github.com/PyO3/setuptools-rust/pull/68) - Add support for pyo3 `0.12`'s `PYO3_PYTHON` setting. [#71](https://github.com/PyO3/setuptools-rust/pull/71)Low8/6/2020
v0.10.6## Changes - Fix `tomlgen_rust` generating invalid `Cargo.toml` files. - Fix `tomlgen_rust` setting wrong path in `.cargo/config`Low11/8/2018
v0.10.5## Changes - Added license file [#41](https://github.com/PyO3/setuptools-rust/pull/41) Low9/10/2018
v0.10.3## Changes - `path` in `RustExtension` now defaults to `Cargo.toml` Low9/6/2018
v0.10.2## Changes - Add `rustc_flags` and `verbose` as options - Adopted black code style - Moved changelog to markdown Low8/9/2018
v0.10.1## Changes * Fix project description rendering on pypiLow6/7/2018
v0.9.2## Changes - Fix `build_rust` crashing on `Cargo.toml` manifests without a `name` key in the `[lib]` section - Fix single quotes not being handled when parsing `Cargo.toml`Low6/6/2018
v0.10.0## Changes * This release significantly improves performanceLow6/5/2018
v0.9.1Changes --------- * Remove unicode_literals future as it does not exists in Python Low3/22/2018
v0.8.4## Changes * Improve compatibility of build_rust with build_ext #28Low2/27/2018
v0.8.3CHANGES ---------- - Ignore strip option when platform is win32 #26Low12/5/2017
v0.8.2Changes --------- - Fix script generation for bdist_wheel Low9/8/2017
0.8.1Changes --------- - Added `native` parameter - Fix script generation for executables Low9/8/2017
0.8.0Changes --------- - Support multiple rust binaries #24Low9/5/2017
v0.7.2Changes --------- - Generate console-script for Binding.Exec #22 - Do not run `cargo check` for `sdist` command #18 - Remove extra python3 file extension for executables. Low9/1/2017
0.7.1Changes --------- - Allow to strip symbols from executable or dynamic library. - Use PyO3 0.2 for exampleLow8/18/2017
0.7.0Changes --------- - Allow to build executable and pack with python package. - Use PyO3 0.1 for example. Low8/11/2017
v0.6.4## Changes - `check` command respects `optional` option - Don't fail when Rust isn't installed while all extensions are optionalLow7/31/2017
v0.6.3## Changes - Fix pypi source distributionLow7/31/2017

Dependencies & License Audit

Loading dependencies...

Similar Packages

setuptoolsMost extensible Python build backend with support for C/C++ extension modules82.0.1
pre-commitA framework for managing and maintaining multi-language pre-commit hooks.v4.6.0
azure-core-tracing-opentelemetryMicrosoft Azure Azure Core OpenTelemetry plugin Library for Pythonazure-template_0.1.0b6187637
spdx-toolsSPDX parser and tools.0.8.5
lacesDjango components that know how to render themselves.0.1.2