Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to execute a function in a file in pymodule

This is an official py03 example.

Python::with_gil(|py| -> PyResult<Py<PyAny>> {
   let py_app = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/app.py"));
   let app: Py<PyAny> = PyModule::from_code(py, py_app, "", "")?
        .getattr("test_function")?
        .into();
    app.call0(py)
})

my local app.py contains the following where the import is the rust module:

import GQLwrapper

print("running app.py")
def test_function:
   print("running test function")

The rust call is running the file and ‘running app.py’ is printed but the test function itself is not being called?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

let app: Py<PyAny> = PyModule::from_code(py, py_app, "", "")?
    .getattr("test_function")?
    .into();

is like python code

import app
app.test_function

That doesn’t execute the function. Instead, you should use call_method0

let app: Py<PyAny> = PyModule::from_code(py, py_app, "", "")?
    .call_method0("test_function")?
    .into();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading