Skip to content

Navigation Menu

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

Appearance settings

Using the stdlib. #4206

MiscStuff839 started this conversation in General
Oct 8, 2022 · 1 comment
Discussion options

I am trying to embed python in rust with RustPython.
I have the code

use rustpython_vm as vm;
use vm::{Settings, stdlib::get_module_inits};

fn main() -> vm::PyResult<()> {
    vm::Interpreter::with_init(Settings::default(), |vm| {vm.add_native_modules(get_module_inits())}).enter(|vm| {
        let code_obj = vm
            .compile(
                "import platform\nprint(platform.release())",
                vm::compiler::Mode::BlockExpr,
                "<embedded>".to_owned(),
            )
            .map_err(|err| vm.new_syntax_error(&err)).unwrap();

        vm.run_code_obj(code_obj, vm.new_scope_with_builtins())?;

        Ok(())
    })
}

This code works when the python does not import anything but it fails with this.
If I need to download the file, is there a way to embed the stdlib?

You must be logged in to vote

Replies: 1 comment

Comment options

I was currently researching a way to achieving the embedding of external libraries together with the embedding of RustPython into a Rust application. So far, apparently, one can use vm.add_frozen(rustpython_pylib::frozen_stdlib()) to add the standard library frozen, which I believe it means you'll be adding a library that can't be changed, like you would normally use it from a Python program. I think that you could also use the feature freeze-stdlib to achieve the same result with less code.

I think there is another way to freeze libraries which I read here, apparently there is a macro called rustpython_vm::py_freeze!(dir = "path/to/dir") that automagically adds the dependencies in a frozen way for you. to see the comment I mentioning, just access this page and search the page for py_freeze this took me straight to the comment.

I believe I'll try this methods soon and I can come back here to more feedback.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.