rust pyo3 bindings import DLL error when use tch

68 views Asked by At

I use PyO3 to write the python bindings with rust.

My code do simple work which is a demo from pyo3 documentation , but simpily add "use tch" in this module, comiple is ok, but failed when runing. These codes are meaningless just for debugging. I keep it as simple as possible.

My LIBTORCH env:

$env:LIBTORCH
C:\Python311\Lib\site-packages\torch
$env:LIBTORCH_USE_PYTORCH
1

My pyo3 codes:


use pyo3::prelude::*;
use tch;

/// 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 pyo3_example(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
    Ok(())
}

And my cargo.toml:

[package]
name = "pyo3-example"
version = "0.1.0"
edition = "2021"

[lib]
name = "pyo3_example"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.19.0"
tch = "0.14.0"

And my test.py:

import pyo3_example as  example
print(example.sum_as_string(2,3))

Then run: python test.py

Reports the folloing error:

import pyo3_example as e
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\jerry\CloudStation\dev\.env\Lib\site-packages\pyo3_example\__init__.py", line 1, in <module>
    from .pyo3_example import *
ImportError: DLL load failed while importing pyo3_example: The specified module could not be found.

If I removed the use tch from my pyo3 codes. It worked.

I tried use pdb, but can't step into the import line.

I want to know:

  1. what's the problem?
  2. or how can I debug it to find which DLL tailed to load?

I tried as above, use pdb to find the missed DLL, but I can't step into the rust codes.

1

There are 1 answers

0
devdoer3 On

I change the runing platform to linux, then it works. So I close the question.

Forget windows.