setuptools-rust is a setuptools extension plugin that simplifies the integration of Rust code compilation and packaging into Python projects, especially those leveraging PyO3. It handles the build process for Rust extensions, making it easier to distribute Python packages with native Rust components. The library is actively maintained, currently at version 1.12.1, with a regular release cadence to support new Python and Rust features.
pip install setuptools-rustVerified import paths — ran on the pinned version, not inferred.
This quickstart provides a `setup.py` example for building a Rust extension using `setuptools-rust`. It defines a `RustExtension` for `my_rust_package.my_module`, pointing to a `Cargo.toml`. Accompanying `pyproject.toml`, `Cargo.toml`, `my_rust_package/__init__.py`, and `src/lib.rs` file structures are commented within the code, demonstrating how to integrate a simple PyO3 Rust function into a Python package. After setting up these files, the project can be built and installed using `pip install .`.
Upgrade your Python environment to 3.9 or newer. If maintaining support for Python 3.8 is necessary, pin `setuptools-rust` to `<1.11.0`.
Remove the `py_limited_api` option from your `RustExtension` definition. The build system will handle it automatically.
Install Rust and Cargo, typically using `rustup install stable`, and ensure the installation directory (e.g., `~/.cargo/bin`) is added to your system's PATH. Verify with `cargo --version`.
Set the environment variable `SETUPTOOLS_RUST_PEP517_USE_BASE_PYTHON=1` before building. This forces `setuptools-rust` to use the base interpreter path, reducing recompilations.
This was a regression in v1.10.0 and fixed in v1.10.1. Upgrade `setuptools-rust` to `1.10.1` or newer. Alternatively, explicitly set `debug=True` in your `RustExtension` configuration if using an affected version.
Install the Rust toolchain using `rustup` by following the instructions on `rustup.rs`. Ensure that `~/.cargo/bin` (or its equivalent for your OS) is added to your system's PATH.
Examine the detailed output immediately following this error message to pinpoint the specific Rust compilation or `cargo` error. This typically involves fixing issues in your Rust code, ensuring all Rust dependencies are correctly specified in `Cargo.toml`, or addressing configuration problems within the Rust project.
Install the package using pip: `pip install setuptools-rust`. If using a `pyproject.toml` with build-system requirements, ensure `setuptools-rust` is listed in `build-system.requires`.
Install essential build tools for your operating system: for Debian/Ubuntu, `sudo apt-get install build-essential`; for macOS, `xcode-select --install`; for Windows, install the 'Desktop development with C++' workload in Visual Studio Installer.
No dependency data recorded yet.