Pyo3: best practice for calling Python from Rust

Playing lately with some scripting languages (rhai, rlua, rustpython and pyo3) to be called from Rust I discover that each has its own implementation idiosyncrasies. With rhai, rlua and rustpython at startup I can define a ScriptEngine that holds the compiled code the interpreter and the scope so that at for each callback (script call) I can do something like:

let interpreter = ... // the code interpreter
let code = ... // the compiled code
let scope = ... // the state passed to and updated by callbacks

let se = ScriptEngine { interpreter, code, scope }; 

impl ScriptEnging {
    pub fn act(
         &mut self,
         action: &str,

// ... and then somewhere else...

let result = se.code, &mut se.scope, action, args);

What is the idiomatic way to do this with pyo3? (e.g. when I try to create a interpreter I run into lifetime issues with the GIL object).

I think I have found the solution in this post.

1 Like

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.