Skip to content

Commit 5955d1f

Browse files
committed
fix sys path
1 parent c9bf8df commit 5955d1f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,20 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
169169

170170
let scope = setup_main_module(vm)?;
171171

172+
// _PyPathConfig_ComputeSysPath0
172173
if !vm.state.config.settings.safe_path {
173-
// TODO: The prepending path depends on running mode
174-
// See https://docs.python.org/3/using/cmdline.html#cmdoption-P
175-
vm.run_code_string(
176-
vm.new_scope_with_builtins(),
177-
"import sys; sys.path.insert(0, '')",
178-
"<embedded>".to_owned(),
179-
)?;
174+
let path0: Option<String> = match &run_mode {
175+
RunMode::Command(_) => Some(String::new()),
176+
RunMode::Module(_) => env::current_dir()
177+
.ok()
178+
.and_then(|p| p.to_str().map(|s| s.to_owned())),
179+
RunMode::Script(_) | RunMode::InstallPip(_) => None, // handled by run_script
180+
RunMode::Repl => Some(String::new()),
181+
};
182+
183+
if let Some(path) = path0 {
184+
vm.insert_sys_path(vm.new_pyobj(path))?;
185+
}
180186
}
181187

182188
let site_result = vm.import("site", 0);

0 commit comments

Comments
 (0)