1

I am trying to use numpy from Rust using pyO3, all inside a conda environment. To get pyO3 to link, I installed the GCC toolchain using conda as well.

I can do simple stuff now like displaying sys.path. However, when I try to import numpy, like so:

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(())
}

the following happens:

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

Interestingly, when I just type python -c "import numpy", the problem does not occur.

I use RUSTFLAGS="-C linker=$CC" cargo +nightly run to build and run the code, if that helps.

0 Answers0