Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
PyO3
Rust bindings for Python. This includes running and interacting with python code from a rust binaries as well as writing native python modules.
- User Guide: stable | master
- API Documentation
A comparison with rust-cpython can be found in the guide.
Usage
Pyo3 supports python 2.7 as well as python 3.5 and up. The minimum required rust version is 1.30.0-nightly 2018-08-18.
You can either write a native python module in rust or use python from a rust binary.
Using rust from python
Pyo3 can be used to generate a native python module.
Cargo.toml
:
[]
= "rust-py"
= "0.1.0"
[]
= "rust_py"
= ["cdylib"]
[]
= "0.4"
= ["extension-module"]
src/lib.rs
extern crate pyo3;
use *;
/// Formats the sum of two numbers as string
/// This module is a python moudle implemented in Rust.
On windows and linux, you can build normally with cargo build --release
. On Mac Os, you need to set additional linker arguments. One option is to compile with cargo rustc --release -- -C link-arg=-undefined -C link-arg=dynamic_lookup
, the other is to create a .cargo/config
with the following content:
[]
= [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
Also on macOS, you will need to rename the output from *.dylib to *.so. On Windows, you will need to rename the output from *.dll to *.pyd.
setuptools-rust
can be used to generate a python package and includes the commands above by default. See examples/word-count and the associated setup.py.
Using python from rust
Add pyo3
this to your Cargo.toml
:
[]
= "0.3"
Example program displaying the value of sys.version
:
extern crate pyo3;
use *;
Examples and tooling
- examples/word-count Counting the occurences of a word in a text file
- hyperjson A hyper-fast Python module for reading/writing JSON data using Rust's serde-json
- rust-numpy Rust binding of NumPy C-API
- pyo3-built Simple macro to expose metadata obtained with the
built
crate as aPyDict
- point-process High level API for pointprocesses as a Python library
License
PyO3 is licensed under the Apache-2.0 license. Python is licensed under the Python License.