Skip to main content

Build and publish crates with pyo3, cffi and uniffi bindings as well as rust binaries as python packages

Project description

Maturin

formerly pyo3-pack

Maturin User Guide Crates.io PyPI discord server

Build and publish crates with pyo3, cffi and uniffi bindings as well as rust binaries as python packages with minimal configuration. It supports building wheels for python 3.8+ on Windows, Linux, macOS and FreeBSD, can upload them to pypi and has basic PyPy and GraalPy support.

Check out the User Guide!

Usage

You can either download binaries from the latest release or install it with pipx or uv:

# pipx
pipx install maturin
# uv
uv tool install maturin

[!NOTE]

pip install maturin should also work if you don't want to use pipx.

There are four main commands:

  • maturin new creates a new cargo project with maturin configured.
  • maturin publish builds the crate into python packages and publishes them to pypi.
  • maturin build builds the wheels and stores them in a folder (target/wheels by default), but doesn't upload them. It's recommended to publish packages with uv using uv publish.
  • maturin develop builds the crate and installs it as a python module directly in the current virtualenv. Note that while maturin develop is faster, it doesn't support all the feature that running pip install after maturin build supports.

maturin doesn't need extra configuration files and doesn't clash with an existing setuptools-rust configuration. You can even integrate it with testing tools such as tox. There are examples for the different bindings in the test-crates folder.

The name of the package will be the name of the cargo project, i.e. the name field in the [package] section of Cargo.toml. The name of the module, which you are using when importing, will be the name value in the [lib] section (which defaults to the name of the package). For binaries, it's simply the name of the binary generated by cargo.

When using maturin build and maturin develop commands, you can compile a performance-optimized program by adding the -r or --release flag.

Python packaging basics

Python packages come in two formats: A built form called wheel and source distributions (sdist), both of which are archives. A wheel can be compatible with any python version, interpreter (cpython and pypy, mainly), operating system and hardware architecture (for pure python wheels), can be limited to a specific platform and architecture (e.g. when using ctypes or cffi) or to a specific python interpreter and version on a specific architecture and operating system (e.g. with pyo3).

When using pip install on a package, pip tries to find a matching wheel and install that. If it doesn't find one, it downloads the source distribution and builds a wheel for the current platform, which requires the right compilers to be installed. Installing a wheel is much faster than installing a source distribution as building wheels is generally slow.

When you publish a package to be installable with pip install, you upload it to pypi, the official package repository. For testing, you can use test pypi instead, which you can use with pip install --index-url https://test.pypi.org/simple/. Note that for publishing for linux, you need to use the manylinux docker container or zig, while for publishing from your repository you can use the PyO3/maturin-action github action.

Mixed rust/python projects

To create a mixed rust/python project, create a folder with your module name (i.e. lib.name in Cargo.toml) next to your Cargo.toml and add your python sources there:

my-project
├── Cargo.toml
├── my_project
│   ├── __init__.py
│   └── bar.py
├── pyproject.toml
├── README.md
└── src
    └── lib.rs

You can specify a different python source directory in pyproject.toml by setting tool.maturin.python-source, for example

pyproject.toml

[tool.maturin]
python-source = "python"
module-name = "my_project._lib_name"

then the project structure would look like this:

my-project
├── Cargo.toml
├── python
│   └── my_project
│       ├── __init__.py
│       └── bar.py
├── pyproject.toml
├── README.md
└── src
    └── lib.rs

[!NOTE]

This structure is recommended to avoid a common ImportError pitfall

maturin will add the native extension as a module in your python folder. When using develop, maturin will copy the native library and for cffi also the glue code to your python folder. You should add those files to your gitignore.

With cffi you can do from .my_project import lib and then use lib.my_native_function, with pyo3 you can directly from .my_project import my_native_function.

Example layout with pyo3 after maturin develop:

my-project
├── Cargo.toml
├── my_project
│   ├── __init__.py
│   ├── bar.py
│   └── _lib_name.cpython-36m-x86_64-linux-gnu.so
├── README.md
└── src
    └── lib.rs

When doing this also be sure to set the module name in your code to match the last part of module-name (don't include the package path):

#[pymodule]
#[pyo3(name="_lib_name")]
fn my_lib_name(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<MyPythonRustClass>()?;
    Ok(())
}

Python metadata

maturin supports PEP 621, you can specify python package metadata in pyproject.toml. maturin merges metadata from Cargo.toml and pyproject.toml, pyproject.toml takes precedence over Cargo.toml.

To specify python dependencies, add a list dependencies in a [project] section in the pyproject.toml. This list is equivalent to install_requires in setuptools:

[project]
name = "my-project"
dependencies = ["flask~=1.1.0", "toml>=0.10.2,<0.11.0"]

You can add so called console scripts, which are shell commands that execute some function in your program in the [project.scripts] section. The keys are the script names while the values are the path to the function in the format some.module.path:class.function, where the class part is optional. The function is called with no arguments. Example:

[project.scripts]
get_42 = "my_project:DummyClass.get_42"

You can also specify trove classifiers in your pyproject.toml under project.classifiers:

[project]
name = "my-project"
classifiers = ["Programming Language :: Python"]

Source distribution

maturin supports building through pyproject.toml. To use it, create a pyproject.toml next to your Cargo.toml with the following content:

[build-system]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"

If a pyproject.toml with a [build-system] entry is present, maturin can build a source distribution of your package when --sdist is specified. The source distribution will contain the same files as cargo package. To only build a source distribution, pass --interpreter without any values.

You can then e.g. install your package with pip install .. With pip install . -v you can see the output of cargo and maturin.

You can use the options compatibility, skip-auditwheel, bindings, strip and common Cargo build options such as features under [tool.maturin] the same way you would when running maturin directly. The bindings key is required for cffi and bin projects as those can't be automatically detected. Currently, all builds are in release mode (see this thread for details).

For a non-manylinux build with cffi bindings you could use the following:

[build-system]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"

[tool.maturin]
bindings = "cffi"
compatibility = "linux"

manylinux option is also accepted as an alias of compatibility for backward compatibility with old version of maturin.

To include arbitrary files in the sdist for use during compilation specify include as an array of path globs with format set to sdist:

[tool.maturin]
include = [{ path = "path/**/*", format = "sdist" }]

There's a maturin sdist command for only building a source distribution as workaround for pypa/pip#6041.

Manylinux and auditwheel

For portability reasons, native python modules on linux must only dynamically link a set of very few libraries which are installed basically everywhere, hence the name manylinux. The pypa offers special docker images and a tool called auditwheel to ensure compliance with the manylinux rules. If you want to publish widely usable wheels for linux pypi, you need to use a manylinux docker image or build with zig.

The Rust compiler since version 1.64 requires at least glibc 2.17, so you need to use at least manylinux2014. For publishing, we recommend enforcing the same manylinux version as the image with the manylinux flag, e.g. use --manylinux 2014 if you are building in quay.io/pypa/manylinux2014_x86_64. The PyO3/maturin-action github action already takes care of this if you set e.g. manylinux: 2014.

maturin contains a reimplementation of auditwheel automatically checks the generated library and gives the wheel the proper platform tag. If your system's glibc is too new or you link other shared libraries, it will assign the linux tag. You can also manually disable those checks and directly use native linux target with --manylinux off.

For full manylinux compliance you need to compile in a CentOS docker container. The pyo3/maturin image is based on the manylinux2014 image, and passes arguments to the maturin binary. You can use it like this:

docker run --rm -v $(pwd):/io ghcr.io/pyo3/maturin build --release  # or other maturin arguments

Note that this image is very basic and only contains python, maturin and stable rust. If you need additional tools, you can run commands inside the manylinux container. See konstin/complex-manylinux-maturin-docker for a small educational example or nanoporetech/fast-ctc-decode for a real world setup.

maturin itself is manylinux compliant when compiled for the musl target.

Examples

  • agg-python-bindings - A Python Library that binds to Asciinema Agg terminal record renderer and Avt terminal emulator
  • ballista-python - A Python library that binds to Apache Arrow distributed query engine Ballista
  • bleuscore - A BLEU score calculation library, written in pure Rust
  • chardetng-py - Python binding for the chardetng character encoding detector.
  • connector-x - ConnectorX enables you to load data from databases into Python in the fastest and most memory efficient way
  • datafusion-python - a Python library that binds to Apache Arrow in-memory query engine DataFusion
  • deltalake-python - Native Delta Lake Python binding based on delta-rs with Pandas integration
  • opendal - OpenDAL Python Binding to access data freely
  • orjson - A fast, correct JSON library for Python
  • polars - Fast multi-threaded DataFrame library in Rust | Python | Node.js
  • pydantic-core - Core validation logic for pydantic written in Rust
  • pyrus-cramjam - Thin Python wrapper to de/compression algorithms in Rust
  • pyxel - A retro game engine for Python
  • roapi - ROAPI automatically spins up read-only APIs for static datasets without requiring you to write a single line of code
  • robyn - A fast and extensible async python web server with a Rust runtime
  • ruff - An extremely fast Python linter, written in Rust
  • rnet - Asynchronous Python HTTP Client with Black Magic
  • rustpy-xlsxwriter: A high-performance Python library for generating Excel files, utilizing the rust_xlsxwriter crate for efficient data handling.
  • tantivy-py - Python bindings for Tantivy
  • tpchgen-cli - Python CLI binding for tpchgen, a blazing fast TPC-H benchmark data generator built in pure Rust with zero dependencies.
  • watchfiles - Simple, modern and high performance file watching and code reload in python
  • wonnx - Wonnx is a GPU-accelerated ONNX inference run-time written 100% in Rust

Contributing

Everyone is welcomed to contribute to maturin! There are many ways to support the project, such as:

  • help maturin users with issues on GitHub and Gitter
  • improve documentation
  • write features and bugfixes
  • publish blogs and examples of how to use maturin

Our contributing notes have more resources if you wish to volunteer time for maturin and are searching where to start.

If you don't have time to contribute yourself but still wish to support the project's future success, some of our maintainers have GitHub sponsorship pages:

License

Licensed under either of:

at your option.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

maturin-1.9.4.tar.gz (213.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

maturin-1.9.4-py3-none-win_arm64.whl (7.0 MB view details)

Uploaded Python 3Windows ARM64

maturin-1.9.4-py3-none-win_amd64.whl (8.2 MB view details)

Uploaded Python 3Windows x86-64

maturin-1.9.4-py3-none-win32.whl (7.3 MB view details)

Uploaded Python 3Windows x86

maturin-1.9.4-py3-none-manylinux_2_31_riscv64.whl (8.5 MB view details)

Uploaded Python 3manylinux: glibc 2.31+ riscv64

maturin-1.9.4-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl (8.9 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ s390x

maturin-1.9.4-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.musllinux_1_1_ppc64le.whl (10.6 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ppc64lemusllinux: musl 1.1+ ppc64le

maturin-1.9.4-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.musllinux_1_1_armv7l.whl (8.1 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARMv7lmusllinux: musl 1.1+ ARMv7l

maturin-1.9.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl (8.1 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64musllinux: musl 1.1+ ARM64

maturin-1.9.4-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl (8.8 MB view details)

Uploaded Python 3manylinux: glibc 2.12+ x86-64musllinux: musl 1.1+ x86-64

maturin-1.9.4-py3-none-manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686.whl (8.3 MB view details)

Uploaded Python 3manylinux: glibc 2.12+ i686musllinux: musl 1.1+ i686

maturin-1.9.4-py3-none-macosx_10_12_x86_64.whl (8.4 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

maturin-1.9.4-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (16.0 MB view details)

Uploaded Python 3macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

maturin-1.9.4-py3-none-linux_armv6l.whl (8.3 MB view details)

Uploaded Python 3

File details

Details for the file maturin-1.9.4.tar.gz.

File metadata

  • Download URL: maturin-1.9.4.tar.gz
  • Upload date:
  • Size: 213.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.9.3

File hashes

Hashes for maturin-1.9.4.tar.gz
Algorithm Hash digest
SHA256 235163a0c99bc6f380fb8786c04fd14dcf6cd622ff295ea3de525015e6ac40cf
MD5 b12c0b3e219ea890377b1e10377d1b2d
BLAKE2b-256 137cb11b870fc4fd84de2099906314ce45488ae17be32ff5493519a6cddc518a

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-win_arm64.whl.

File metadata

  • Download URL: maturin-1.9.4-py3-none-win_arm64.whl
  • Upload date:
  • Size: 7.0 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.9.3

File hashes

Hashes for maturin-1.9.4-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 7a6f980a9b67a5c13c844c268eabd855b54a6a765df4b4bb07d15a990572a4c9
MD5 b9f18ca40968f1b2bad715caef37330b
BLAKE2b-256 3f258320fc2591e45b750c3ae71fa596b47aefa802d07d6abaaa719034a85160

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-win_amd64.whl.

File metadata

  • Download URL: maturin-1.9.4-py3-none-win_amd64.whl
  • Upload date:
  • Size: 8.2 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.9.3

File hashes

Hashes for maturin-1.9.4-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 8e450bb2c9afdf38a0059ee2e1ec2b17323f152b59c16f33eb9c74edaf1f9f79
MD5 e25c95530544a7fcfda9915dec6421ce
BLAKE2b-256 1414f86d0124bf1816b99005c058a1dbdca7cb5850d9cf4b09dcae07a1bc6201

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-win32.whl.

File metadata

  • Download URL: maturin-1.9.4-py3-none-win32.whl
  • Upload date:
  • Size: 7.3 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.9.3

File hashes

Hashes for maturin-1.9.4-py3-none-win32.whl
Algorithm Hash digest
SHA256 ed2e54d132ace7e61829bd49709331007dd9a2cc78937f598aa76a4f69b6804d
MD5 1f3e735b7d13b3b6c6aeb146ffcc5cf2
BLAKE2b-256 43e3f304c3bdc3fba9adebe5348d4d2dd015f1152c0a9027aaf52cae0bb182c8

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-manylinux_2_31_riscv64.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-manylinux_2_31_riscv64.whl
Algorithm Hash digest
SHA256 273f879214f63f79bfe851cd7d541f8150bdbfae5dfdc3c0c4d125d02d1f41b4
MD5 b532b55ff1a92c10a3e6ca9adededb3d
BLAKE2b-256 7e27153ad15eccae26921e8a01812da9f3b7f9013368f8f92c36853f2043b2a3

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 368e958468431dfeec80f75eea9639b4356d8c42428b0128444424b083fecfb0
MD5 e2642c1d72c6723fb0079aa5d4282e8b
BLAKE2b-256 3c4b19ad558fdf54e151b1b4916ed45f1952ada96684ee6db64f9cd91cabec09

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.musllinux_1_1_ppc64le.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.musllinux_1_1_ppc64le.whl
Algorithm Hash digest
SHA256 ef20ffdd943078c4c3699c29fb2ed722bb6b4419efdade6642d1dbf248f94a70
MD5 906208defb540b88925dde02b446f344
BLAKE2b-256 45e8c623955da75e801a06942edf1fdc4e772a9e8fbc1ceebbdc85d59584dc10

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.musllinux_1_1_armv7l.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.musllinux_1_1_armv7l.whl
Algorithm Hash digest
SHA256 08dc86312afee55af778af919818632e35d8d0464ccd79cb86700d9ea560ccd7
MD5 52a8e777aade2402b6d38d583d5559fa
BLAKE2b-256 73eeca7308832d4f5b521c1aa176d9265f6f93e0bd1ad82a90fd9cd799f6b28c

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 68b7b833b25741c0f553b78e8b9e095b31ae7c6611533b3c7b71f84c2cb8fc44
MD5 ab7159b1ae5e2b67a5138602534dbec4
BLAKE2b-256 b42e26fa7574f01c19b7a74680fd70e5bae2e8c40fed9683d1752e765062cc2b

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 a0868d52934c8a5d1411b42367633fdb5cd5515bec47a534192282167448ec30
MD5 af0fd0bc4e616396d954a49911406ac9
BLAKE2b-256 d246001fcc5c6ad509874896418d6169a61acd619df5b724f99766308c44a99f

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-manylinux_2_12_i686.manylinux2010_i686.musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 1bb2aa0fa29032e9c5aac03ac400396ddea12cadef242f8967e9c8ef715313a1
MD5 ed94588fd86afe041379f22d7acce2ce
BLAKE2b-256 518ec56176dd360da9650c62b8a5ecfb85432cf011e97e46c186901e6996002e

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4227d627d8e3bfe45877a8d65e9d8351a9d01434549f0da75d2c06a1b570de58
MD5 bd38427ae0a6e4d77539e5a8f5837cdc
BLAKE2b-256 844e401ff5f3cfc6b123364d4b94379bf910d7baee32c9c95b72784ff2329357

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 f3837bb53611b2dafa1c090436c330f2d743ba305ef00d8801a371f4495e7e1b
MD5 af1f97c6ba170df8abb6723879e905c3
BLAKE2b-256 f4edc8ec68b383e50f084bf1fa9605e62a90cd32a3f75d9894ed3a6e5d4cc5b3

See more details on using hashes here.

File details

Details for the file maturin-1.9.4-py3-none-linux_armv6l.whl.

File metadata

File hashes

Hashes for maturin-1.9.4-py3-none-linux_armv6l.whl
Algorithm Hash digest
SHA256 6ff37578e3f5fdbe685110d45f60af1f5a7dfce70a1e26dfe3810af66853ecae
MD5 65281a6975208fceb96da285706dee13
BLAKE2b-256 f2900d99389eea1939116fca841cad0763600c8d3183a02a9478d066736c60e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page