From c12cd7fc5fe2a4b2308f370b3d9e39b7c5e88c89 Mon Sep 17 00:00:00 2001 From: lakshmip03 Date: Fri, 18 Sep 2026 14:35:55 +0530 Subject: [PATCH 1/2] fix: improve error message when RUSTPYTHONPATH not loaded in Settings When embedding RustPython and RUSTPYTHONPATH is set but not loaded into Settings::path_list, the error message now explains exactly how to fix it using Settings::with_path() or path_list directly. Fixes #4988 Signed-off-by: lakshmip03 --- crates/vm/src/vm/mod.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/vm/src/vm/mod.rs b/crates/vm/src/vm/mod.rs index ac6465e2872..f508646b785 100644 --- a/crates/vm/src/vm/mod.rs +++ b/crates/vm/src/vm/mod.rs @@ -1143,7 +1143,18 @@ impl VirtualMachine { } else if path_contains_env { "RUSTPYTHONPATH or PYTHONPATH is set, but it doesn't contain the encodings library. If you are customizing the RustPython vm/interpreter, try adding the stdlib directory to the path. If you are developing the RustPython interpreter, it might be a bug during development." } else { - "RUSTPYTHONPATH or PYTHONPATH is set, but it wasn't loaded to `PyConfig::paths::module_search_paths`. If you are going to customize the RustPython vm/interpreter, those environment variables are not loaded in the Settings struct by default. Please try creating a customized instance of the Settings struct. If you are developing the RustPython interpreter, it might be a bug during development." + "RUSTPYTHONPATH or PYTHONPATH is set, but it wasn't loaded to `PyConfig::paths::module_search_paths`. \ +Environment variables are not loaded into the Settings struct by default when embedding RustPython. \ +To fix this, add the stdlib path manually:\n\ +\n\ + let settings = rustpython_vm::Settings::default().with_path(\"/path/to/stdlib\".to_owned());\n\ +\n\ +Alternatively, set the RUSTPYTHONPATH environment variable and load it explicitly:\n\ +\n\ + settings.path_list.extend(std::env::var(\"RUSTPYTHONPATH\").unwrap_or_default().split(':').map(String::from));\n\ +\n\ +If you are developing the RustPython interpreter itself, this might be a bug during development." + }; let mut msg = format!( From 692dcb2b03686071c68046e49971fdb5d6948921 Mon Sep 17 00:00:00 2001 From: lakshmip03 Date: Mon, 21 Sep 2026 13:18:01 +0530 Subject: [PATCH 2/2] fix: address review feedback on error message - Remove leading whitespace on continuation line - Update wording: 'If RUSTPYTHONPATH is already set, explicitly load it to path' - Use std::env::split_paths instead of split(':') for cross-platform support Signed-off-by: lakshmip03 --- crates/vm/src/vm/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/vm/src/vm/mod.rs b/crates/vm/src/vm/mod.rs index f508646b785..d1bb2e97d69 100644 --- a/crates/vm/src/vm/mod.rs +++ b/crates/vm/src/vm/mod.rs @@ -1149,9 +1149,12 @@ To fix this, add the stdlib path manually:\n\ \n\ let settings = rustpython_vm::Settings::default().with_path(\"/path/to/stdlib\".to_owned());\n\ \n\ -Alternatively, set the RUSTPYTHONPATH environment variable and load it explicitly:\n\ +If `RUSTPYTHONPATH` is already set, explicitly load it to path:\n\ \n\ - settings.path_list.extend(std::env::var(\"RUSTPYTHONPATH\").unwrap_or_default().split(':').map(String::from));\n\ + settings.path_list.extend(\n\ + std::env::split_paths(&std::env::var_os(\"RUSTPYTHONPATH\").unwrap_or_default())\n\ + .map(|path| path.to_string_lossy().into_owned()),\n\ + );\n\ \n\ If you are developing the RustPython interpreter itself, this might be a bug during development."