Making a CLI REPL #2856
-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
After asking for some help to some rust folks, they found out that Python's Here follows the solution code: pub fn create_interpreter() -> PyResult<()> {
let interp = Python::with_gil(|py| {
let sys = py.import("sys")?;
let version: String = sys.getattr("version")?.extract()?;
let locals = [("os", py.import("os")?)].into_py_dict(py);
let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
let user: String = py.eval(code, None, Some(&locals))?.extract()?;
println!("Hello {}, I'm Python {}", user, version);
py.run("import code; code.interact('')", None, None)
});
interp
} Now for some takes on the matter, I think it would be good that this could be addressed to implementation on Pyo3, and possibly addressed in the docs, as i took some time to find a solution for it and personally its a case i come across often enough to be face-value documented There is also entry-points which can also be beneficial to have which some of them are here https://docs.python.org/3/c-api/veryhigh.html |
Beta Was this translation helpful? Give feedback.
After asking for some help to some rust folks, they found out that Python's module which essentially is most of the REPL behavior.
Here follows the solution code: