我正在尝试使用 pyO3 使用 Rust 中的 numpy,所有这些都在 conda 环境中。为了让 pyO3 链接,我也使用 conda 安装了 GCC 工具链。
我现在可以做一些简单的事情,比如显示sys.path
。但是,当我尝试导入 numpy 时,如下所示:
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
fn main() -> PyResult<()> {
let gil = Python::acquire_gil();
let py = gil.python();
let r = py.import("numpy");
match r {
Ok(_) => println!("OK"),
Err(e) => e.print(py),
}
Ok(())
}
发生以下情况:
Traceback (most recent call last):
File "<HOME>/anaconda3/envs/pyrust/lib/python3.7/site-packages/numpy/__init__.py", line 140, in <module>
from . import _distributor_init
File "<HOME>/anaconda3/envs/pyrust/lib/python3.7/site-packages/numpy/_distributor_init.py", line 33, in <module>
with RTLD_for_MKL():
File "<HOME>/anaconda3/envs/pyrust/lib/python3.7/site-packages/numpy/_distributor_init.py", line 18, in __enter__
import ctypes
File "<HOME>/anaconda3/envs/pyrust/lib/python3.7/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ImportError: <HOME>/anaconda3/envs/pyrust/lib/python3.7/lib-dynload/_ctypes.cpython-37m-x86_64-linux-gnu.so: undefined symbol: PyUnicode_FromFormat
有趣的是,当我只键入 时python -c "import numpy"
,并没有出现问题。
如果有帮助,我会使用它RUSTFLAGS="-C linker=$CC" cargo +nightly run
来构建和运行代码。