single |
# Setuptools plugin for Rust extensions
![example workflow]
[pypi package]
[readthedocs]
[![code style: black]](https://github.com/ambv/black)
`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.
## Setup
For a complete example, see
[html-py-ever].
First, you need to create a bunch of files:
### setup.py
```python
from setuptools import setup
from setuptools_rust import Binding, RustExtension
setup(
name="hello-rust",
version="1.0",
rust_extensions=[RustExtension("hello_rust.hello_rust",
binding=Binding.PyO3)],
packages=["hello_rust"],
# rust extensions are not zip safe, just like C-extensions.
zip_safe=False,
)
```
For a complete reference of the options supported by the `RustExtension`
class, see the
[API reference].
### MANIFEST.in
This file is required for building source distributions
```text
include Cargo.toml
recursive-include src *
```
### pyproject.toml
```toml
[build-system]
requires = ["setuptools", "wheel", "setuptools-rust"]
```
### build-wheels.sh
```bash
#!/bin/bash
set -ex
curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain stable -y
export PATH="$HOME/.cargo/bin:$PATH"
cd /io
for PYBIN in /opt/python/cp{35,36,37,38,39}*/bin; do
"${PYBIN}/pip" install -U setuptools wheel setuptools-rust
"${PYBIN}/python" setup.py bdist_wheel
done
for whl in dist/*.whl; do
auditwheel repair "$whl" -w dist/
done
```
## Usage
You can use same commands as for c-extensions. For example:
```
>>> python ./setup.py develop
running develop
running egg_info
writing hello-rust.egg-info/PKG-INFO
writing top-level names to hello_rust.egg-info/top_level.txt
writing dependency_links to hello_rust.egg-info/dependency_links.txt
reading manifest file 'hello_rust.egg-info/SOURCES.txt'
writing manifest file 'hello_rust.egg-info/SOURCES.txt'
running build_ext
running build_rust
cargo build --manifest-path extensions/Cargo.toml --features python3
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Creating /.../lib/python3.6/site-packages/hello_rust.egg-link (link to .)
Installed hello_rust
Processing dependencies for hello_rust==1.0
|