Howto debug pyo3 module

I've written a Python module in Rust using pyo3.
Now I want to debug the module at Rust level, but I don't know how an example should look like.

I've created the following example:

use pyo3::prelude::*;

fn main() -> Result<(), ()> {
    Python::with_gil(|py| {
        main_(py).map_err(|e| {
          // We can't display Python exceptions via std::fmt::Display,
          // so print the error here manually.

fn main_(py: Python) -> PyResult<()> {
    let a2l_rs = py.import("a2l_rs")?;
    let a2lFile = a2l_rs.Instance("example.a2l");
    let result = a2lFile.load();

It fails to compile with the following error message:

error[E0599]: no method named `Instance` found for reference `&pyo3::prelude::PyModule` in the current scope
  --> examples/
15 |     let a2lFile = a2l_rs.Instance("example.a2l");
   |                          ^^^^^^^^ method not found in `&pyo3::prelude::PyModule`

How can I make the methods in my module visible to the example?

The error is saying Instance() isn't a method on a python module.

If the a2l_rs python module contains some callable called Instance, you will need to look it up with PyModule::get().

PyModule is still a Rust object, so it doesn't have the methods of the python module that it represents. So you cannot call the module like that.

For more examples also see PyModule in pyo3::types - Rust

Thank you very much for the replies. The example now compiles, but linking fails with lot's of undefined references to Python functions.

/home/martin/.cargo/registry/src/ undefined reference to `PyTuple_New'
          /usr/bin/ld: /home/martin/.cargo/registry/src/ undefined reference to `PyTuple_SetItem'
          /usr/bin/ld: /home/martin/Rust/a2l-rs/target/debug/examples/test_reader-4035dd3d13a547ee.4wnx3ycyz7eo6cxv.rcgu.o: in function `pyo3::ffi::object::Py_DECREF':
          /home/martin/.cargo/registry/src/ undefined reference to `_Py_Dealloc'
          /usr/bin/ld: /home/martin/Rust/a2l-rs/target/debug/examples/test_reader-4035dd3d13a547ee.57e89mka91xcnn3t.rcgu.o: in function `pyo3::types::any::PyAny::call':
          /home/martin/.cargo/registry/src/ undefined reference to `PyObject_Call'

My Cargo.toml in examples directory:

name = "examples"
version = "0.1.0"
authors = []
edition = "2018"

name = "test_reader"
path = ""

version = "0.13.2"
features = ["auto-initialize"]

What is wrong with the Cargo.toml?

You might be missing some things, compare your cargo.toml with the one at the api docs: pyo3 - Rust

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.