- Notifications
You must be signed in to change notification settings - Fork1.4k
How to properly embed Python libraries/modules with embedded RustPython?#4407
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I didn't put it in the title because I thought I might be wrong, but I think that the way to do it is to freeze the dependencies, as described in the readme:
that's in the section directed to thebuilding the WASI file, but I believe it might work for non-wasm targets. I'm trying to embed RustPython into my application and I'm currently just writing tests and simples examples to get the hang of the library. In my application, I intend to use sympy, so I tried to write a example on how I could achieve it. it compiles fine, but when I run the executable I get an exception saying that there is no sympy module. In this example I tried to freeze the Python dependencies with the intention to embed them into the binary too but I might be doing it wrong or maybe this isn't supposed to be used this way. The example application I basically copied the hello_embed.rs example and made some modifications. Here is the source code: use rustpython_vmas vm;use vm::VirtualMachine;fnmain(){vm::Interpreter::with_init(Default::default(), init_vm).enter(|vm|{let scope = vm.new_scope_with_builtins();let code_obj = vm.compile(r#"import sympy as spx = sp.Symbol('x')y = sp.Symbol('y')print(((x+y)**5).expand())"#,vm::compiler::Mode::Exec,"<embedded>".to_owned(),)// end of the `compile` function.unwrap_or_else(|err|{// print the exception and exits the application vm.print_exception(vm.new_syntax_error(&err)); std::process::exit(0)});ifletErr(exception) = vm.run_code_obj(code_obj, scope){ vm.print_exception(exception);}})// end of the `enter` function}fninit_vm(vm:&mutVirtualMachine){vm.add_frozen(rustpython_pylib::frozen_stdlib());vm.add_frozen(rustpython_vm::py_freeze!(dir ="deps/mpmath"));// sympy depends on this library so I had to embed it toovm.add_frozen(rustpython_vm::py_freeze!(dir ="deps/sympy"));} and this is the error I get when I try to run this: which is basically saying there is no sympy module but as I understood, there should be an embedded one. Another thing that might be important is that I got the source code for those libraries from the pip installation, I simply copied pasted the folder of the library into the folders described in the `init_vm´ function I defined. I figured this could work since I sawthis comment, but that's also targeted at wasm. Can you guys help me or point me towards possible sources of this problem? my thoughts on this is:
Edit: I also tried to set RUSTPYTHONPATH, but it didn't work, I believe this is expected becauce RustPython is embedded in the application. I also forgot, but thanks for this amazing project, the API is clean and comprehensive! congrats!🚀🚀🚀🦀🦀🦀 |
BetaWas this translation helpful?Give feedback.
All reactions
👍 2