Py03: calling Rust function from Python code



I’m quite new to rust and I’ve been trying to find some documentation/examples regarding python/rust bindings with pyO3. However, I couldn’t find any helpful information concerning those imports without using a python module and rust setuptools.
The documentation implies it’s possible without giving any clue on how to do it.

I’ve been trying to use the example given by Ben Frederickson in this article. without success, the code doesn’t compile as it is (modinit argument problems).
The code given here by the Pyo3 documentation doesn’t compile either (the pymodule attribute being unknown to the compiler).

Do you know were I could find working and preferably up to date pyo3 examples/documentation for python/rust bindings without having to create a python package using rust setuptools?



May be you use stable instead of nightly compiler?

Also you can try, it works with stable compiler


Hi, what have you tried to compile exactly? The code from the PyO3 documentation you’ve posted should work.

You’ll need to add this to your Cargo.toml, then it should compile:

name = "rust2py"
crate-type = ["cdylib"]

version = "0.4"
features = ["extension-module"]

You’ll get a .so in target/debug, then you can go into that folder, start python and import rust2py and call that function.