Py03: calling Rust function from Python code


#1

Hello,

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?

Thanks!


#2

May be you use stable instead of nightly compiler?

Also you can try, it works with stable compiler

https://github.com/dgrunwald/rust-cpython


#3

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:

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

[dependencies.pyo3]
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.