@@ -169,16 +169,9 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
169169
170170 let scope = setup_main_module ( vm) ?;
171171
172- 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- ) ?;
180- }
181-
172+ // Import site first, before setting sys.path[0]
173+ // This matches CPython's behavior where site.removeduppaths() runs
174+ // before sys.path[0] is set, preventing '' from being converted to cwd
182175 let site_result = vm. import ( "site" , 0 ) ;
183176 if site_result. is_err ( ) {
184177 warn ! (
@@ -187,6 +180,22 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
187180 ) ;
188181 }
189182
183+ // _PyPathConfig_ComputeSysPath0 - set sys.path[0] after site import
184+ if !vm. state . config . settings . safe_path {
185+ let path0: Option < String > = match & run_mode {
186+ RunMode :: Command ( _) => Some ( String :: new ( ) ) ,
187+ RunMode :: Module ( _) => env:: current_dir ( )
188+ . ok ( )
189+ . and_then ( |p| p. to_str ( ) . map ( |s| s. to_owned ( ) ) ) ,
190+ RunMode :: Script ( _) | RunMode :: InstallPip ( _) => None , // handled by run_script
191+ RunMode :: Repl => Some ( String :: new ( ) ) ,
192+ } ;
193+
194+ if let Some ( path) = path0 {
195+ vm. insert_sys_path ( vm. new_pyobj ( path) ) ?;
196+ }
197+ }
198+
190199 // Enable faulthandler if -X faulthandler, PYTHONFAULTHANDLER or -X dev is set
191200 // _PyFaulthandler_Init()
192201 if vm. state . config . settings . faulthandler {
0 commit comments