0

I want to write part of a python module in rust (with PYo3) but also partly in python, so something like...

src/utils.rs:

use pyo3::prelude::*;

#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
    Ok((a + b).to_string())
}

#[pymodule]
fn nvm(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
    Ok(())
}

src/main.py:

from .utils import sum_as_string

...

Mostly just curious if this is possible with pyo3 or if I have to write everything in rust, cuz I've seen something like this done with C/Cython (example).

Middledot
  • 82
  • 1
  • 7
  • 1
    Well, yeah, that's what pyo3 is for. And your example seems to be taken from their [README](https://github.com/PyO3/pyo3/blob/main/README.md) directly. What's the problem? – wim Sep 23 '22 at 19:33
  • @wim I don't know how to have a pyo3 project with certain python files and certain rust files where I can import rust files from python and vice versa (within the module). Also the example was just meant to show what I mean to do. – Middledot Sep 23 '22 at 19:43

1 Answers1

0

Yes, this is possible. Maturin is the project you'll want to look through for doing this as pyo3 is simply the bindings to the python runtime. You can see documentation on a mixed python/rust project here

wannabe
  • 192
  • 2
  • 5