py_load

Function py_load 

Source
pub fn py_load(py: Python<'_>, fpath: &str) -> RUMResult<RUMPyModule>
Expand description

Load a python module from a given file path!

ยงExample Usage

    use compact_str::format_compact;
use pyo3::Python;
    use pyo3::types::PyModule;
    use crate::rumtk_core::scripting::python_utils::RUMPyModule;
    use crate::rumtk_core::scripting::python_utils::{py_load};
    use crate::rumtk_core::strings::RUMString;
    use uuid::Uuid;

    let expected: &str = "print('Hello World!')\ndef test():\n\treturn 'Hello'";
    let fpath: RUMString = format_compact!("/tmp/{}.py", Uuid::new_v4());
    std::fs::write(&fpath, expected.as_bytes()).expect("Failure to write test module.");

    Python::attach(|py| {
        let py_obj: RUMPyModule = py_load(py, &fpath).expect("Failure to load module!");
    });
    std::fs::remove_file(&fpath).unwrap()