Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to properly embed Python libraries/modules with embedded RustPython?#4407

Unanswered
Rafael-Conde asked this question inQ&A
Discussion options

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:

Note: we use the freeze-stdlib to include the standard library inside the binary. You also have to run once rustup target add wasm32-wasi

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:

Traceback (most recent call last):  File "<embedded>", line 1, in <module>ModuleNotFoundError: No module named 'sympy'

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:

  • that, it might be available only to wasm
  • the modules I'm trying to embed might be too big and that can cause problems
  • since sympy and mpmath are in the deps folder, I also already tried to only add thevm.add_frozen(rustpython_vm::py_freeze!(dir = "deps")); but it didn't work as well.

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!🚀🚀🚀🦀🦀🦀

You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@Rafael-Conde

[8]ページ先頭

©2009-2025 Movatter.jp