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 run a python project by using rustpython_vm?#5721

ClosedUnanswered
qzd1989 asked this question inQ&A
Discussion options

my rust project structure:

- examples/  - rustpython.rs- python_project/  - main.py  - utils/    - helper.py- src/  - main.rsCargo.toml

Cargo.toml

[dependencies]rustpython-stdlib ="0.4.0"rustpython-vm ="0.4.0"

python_project/main.py

importtimeprint("Hello, World!",time.time())fromutils.helperimportgreet#this line causes error.print(greet("World"))

python_project/utils/helper.py

defgreet(name):returnf"Hello,{name}!"

examples/rustpython.rs

use rustpython_vmas vm;fnmain() -> vm::PyResult<()>{let vm = vm::Interpreter::with_init(Default::default(), |vm|{        vm.add_native_modules(rustpython_stdlib::get_module_inits());});    vm.enter(|vm|{        vm.insert_sys_path(vm.new_pyobj("python_project"))?;let scope = vm.new_scope_with_builtins();let source_path ="python_project/main.py";let source = std::fs::read_to_string(source_path).expect("unable to read main.py");let code_obj = vm.compile(&source, vm::compiler::Mode::Exec, source_path.into()).map_err(|err| vm.new_syntax_error(&err,Some(&source)))?;ifletErr(err) = vm.run_code_obj(code_obj, scope){            vm.print_exception(err);}Ok(())})}

cargo run --example rustpython

Hello, World! 1745159203.3709767Traceback (most recent call last):  File "python_project/main.py", line 3, in <module>    from utils.helper import greetModuleNotFoundError: No module named 'utils'

thank you sir, really need help, it seemsinsert_sys_path doesn't work or I didn't find the right way?

You must be logged in to vote

Replies: 4 comments 5 replies

Comment options

Would you share the result ofvm.print_exception(err)?

You must be logged in to vote
3 replies
@qzd1989
Comment options

I update the rust code and output.
the error is "ModuleNotFoundError: No module named 'utils'"

@youknowone
Comment options

maybe missing__init__.py underutils? If it doesn't help, could you share the crate you made?

@qzd1989
Comment options

maybe missing__init__.py underutils? If it doesn't help, could you share the crate you made?

yes, I tried, but not work. here is my code:

https://github.com/qzd1989/rust-rustpython-test

thank you, sir.

Comment options

Ensure the project works with regular python before attempting to use rustpython

You must be logged in to vote
1 reply
@qzd1989
Comment options

I run the same code in a installed python env, it works, and it doesn't work in an embed python env(same error, can't find module).
it semms that rustpython doesn't load the path of the project into sys.path.

and this line:

vm.insert_sys_path(vm.new_pyobj("python_project"))?;

I think it should work but it doesn't work...

Comment options

I have used embeded cpython instead of rustpython.

You must be logged in to vote
0 replies
Comment options

Okay, found problem.

rustpython-vm = "0.4.0"
have to be
rustpython-vm = { version = "0.4.0", features = ["importlib] }

I regard this as a user experience bug not to warn common pitfall easy.

You must be logged in to vote
1 reply
@qzd1989
Comment options

thank you sir, I think the code comments and examples in RustPython are too few, making it unfriendly for beginners :(

i have used embeded cpython, hope rustpython will become better.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@qzd1989@youknowone@arihant2math

[8]ページ先頭

©2009-2025 Movatter.jp