Extending python with rust

Hey everyone!
I am new to rust and I have a question about extending python with rust.
I have experience in extending Python with the C libraries. It was very easy to write the wrapper for a lot of C libraries. All I need is the DLL and the header files and then use Cython to wrap the C code.

A lot of the C libraries that I use are "proprietary", i.e., I don't have to the code itself. I only have header files and the DLL.

Is there a way to extend Python with Rust similar to the above method? Just by having the DLL file and (in this case) the function signature (function name and arguments)?

All the methods I have seen so far require updating the rust code, which is inconvenient if you don't have the code.

Thanks in advance,

How about GitHub - PyO3/pyo3: Rust bindings for the Python interpreter?

1 Like

Thanks for your response!

I looked into Pyo3 and I "think" Pyo3 uses rust nightly. I was looking for a standard/stable way. I am just exploring options.
Also, we need to have access to the Rust code to write the wrapper or to define the python functions/arguments, etc. But the second point can be solved by writing the wrapper in rust (call the DLL in rust and wrap it!).

Rust nightly is less unstable than it sounds - also the key is to pin your project to specific nighty versions (which are tagged by date).

I think they were using nightly for attribute macros at one point, but those have been stabilised for a couple years now. For one data point, my computer has Rust 1.59.0 on it (latest stable) and I can compile a project that uses PyO3 with no issues.

Often what you'll do is write a wrapper crate that imports the original Rust crate and exposes simplified a Python interface.

We use PyO3 for this thing at work and it's really easy.

1 Like

I am using PyO3 in production with rustc 1.53 stable. It most definitely does not require nightly.

1 Like

Thank you for the recommendation! I will give it a try :slight_smile:

1 Like