我最近遇到了 PyO3,我已经成功地从PyO3 repo构建了示例代码。
货运.toml
[package]
name = "string-sum"
version = "0.1.0"
edition = "2018"
[lib]
name = "string_sum"
crate-type = ["cdylib"]
[dependencies.pyo3]
version = "0.12.1"
features = ["extension-module"]
库文件
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}
/// A Python module implemented in Rust.
#[pymodule]
fn string_sum(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}
该构建创建了两个文件libstring_sum.d和libstring_sum.so,然后我创建了一个测试 python 脚本并尝试使用构建这个 rust 绑定,import libstring_sum
但它给出了这个错误
ImportError:动态模块没有定义模块导出函数(PyInit_libstring_sum)