internal/tools/: setuptools-rust-1.9.0 metadata and description

Simple index

Setuptools Rust extension plugin

author_email Nikolay Kim <>
  • Topic :: Software Development :: Version Control
  • License :: OSI Approved :: MIT License
  • Intended Audience :: Developers
  • Programming Language :: Python :: 3
  • Programming Language :: Python :: 3.8
  • Programming Language :: Python :: 3.9
  • Programming Language :: Python :: 3.10
  • Programming Language :: Python :: 3.11
  • Development Status :: 5 - Production/Stable
  • Operating System :: POSIX
  • Operating System :: MacOS :: MacOS X
  • Operating System :: Microsoft :: Windows
description_content_type text/markdown
keywords distutils,setuptools,rust
  • Homepage,
  • Repository,
  • Documentation,
  • Changelog,
  • setuptools >=62.4
  • semantic-version <3,>=2.8.2
  • tomli >=1.2.1 ; python_version < "3.11"
requires_python >=3.8
File Tox results History
304 KB
26 KB
Python Wheel

Setuptools plugin for Rust extensions

github actions pypi package readthedocs Ruff

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.


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.

├── python
│   └── hello_world
│       └──
└── rust

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
requires = ["setuptools", "setuptools-rust"]
build-backend = "setuptools.build_meta"

name = "hello-world"
version = "1.0"

# Pure Python packages/modules
find = { where = ["python"] }

# 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 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:

# Cargo.toml
name = "hello-world"
version = "0.1.0"
edition = "2021"

pyo3 = "0.20.3"

name = "_lib"  # private module to be nested into Python package,
               # needs to match the name of the function with the `[#pymodule]` attribute
path = "rust/"
crate-type = ["cdylib"]  # required for shared library for Python to import from.

# See more keys and their definitions at
# See also PyO3 docs on writing Cargo.toml files at

You will also need to tell Setuptools that the Rust files are required to build your project from the source distribution. That can be done either via (see example below) or via a plugin like setuptools-scm.

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:

# 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 .
>>> import hello_world
# ... try running something from your new extension module ...
# ... better write some tests with pytest ...

Next steps and final remarks