Calling local python module from rust #3718
-
I'm attempting to call a function from a local python module in my rust code. (full code below). I've activated a virtual environment and installed the module with
However, when I run my test Iget the error below. Note that I have verified the installation of my module by entering the python REPL and importing it there, with no issues. Any guidance would be much appreciated :)
// adder/src/lib.rs
use ::pyo3::prelude::*;
pub fn add_numpy(a: i32, b: i32) -> PyResult<i32> {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
let numpy_add = PyModule::import(py, "deconflict")?;
let sum: i32 = numpy_add.getattr("add")?.call1((a, b))?.extract()?;
Ok(sum)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn py_binding() {
let res = add_numpy(40, 42);
match res {
Ok(val) => assert_eq!(val, 42),
Err(err) => panic!("{}", err),
}
}
} # deconflict/deconflict/add.py
import numpy as np
from dataclasses import dataclass
@dataclass
class Response:
label: str
sum: int
def add(a: int, b: int) -> Response:
array = np.array([a, b])
return Response(label="hello world", sum=array.sum()) # deconflict/deconflict/__init__.py
from .add import add # pyproject.toml
[tool.poetry]
name = "deconflict"
version = "0.1.0"
description = "Deconfliction"
authors = ["shot-codes <[email protected]>"]
readme = "README.md"
packages = [{ include = "deconflict" }]
[tool.poetry.dependencies]
python = "^3.11"
numpy = "^1.26.2"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api" |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Sorry for the slow reply. Maybe #3717 (comment) is relevant to you? |
Beta Was this translation helpful? Give feedback.
Sorry for the slow reply. Maybe #3717 (comment) is relevant to you?