@@ -89,19 +89,25 @@ def rewrite_standard_library_sys_path():
89
89
skip_rewrite = value == exe_dir # don't fix the current executable location, notably on Windows this gets added
90
90
skip_rewrite = skip_rewrite # ___SKIP_REWRITE____
91
91
if not skip_rewrite :
92
- if dir_starts_with (value , exe_dir ):
93
- # content inside the exe folder needs to remap to original executables folder
94
- orig_exe_folder = base_executable [: base_executable .rfind (sep )]
95
- value = "{}{}" .format (orig_exe_folder , value [len (exe_dir ) :])
96
- elif dir_starts_with (value , prefix ):
97
- value = "{}{}" .format (base_prefix , value [len (prefix ) :])
98
- elif dir_starts_with (value , exec_prefix ):
99
- value = "{}{}" .format (base_exec_prefix , value [len (exec_prefix ) :])
100
- sys .path [at ] = value
92
+ sys .path [at ] = translate_directory (
93
+ value , base_executable , exe_dir , exec_prefix , base_prefix , prefix , base_exec_prefix
94
+ )
101
95
96
+ # Now that we can access os.environ["PYTHONPATH"], we can revert our
97
+ # rewrite of thoses paths done above
98
+ import os
102
99
103
- def dir_starts_with (directory , prefix ):
104
- return directory == prefix or directory .startswith (prefix + sep )
100
+ python_paths = os .environ .get ("PYTHONPATH" , "" ).split (":" )
101
+ for ppath in map (abs_path , python_paths ):
102
+ translated_ppath = translate_directory (
103
+ ppath , base_executable , exe_dir , exec_prefix , base_prefix , prefix , base_exec_prefix
104
+ )
105
+ try :
106
+ at = sys .path .index (translated_ppath )
107
+ except ValueError :
108
+ pass
109
+ else :
110
+ sys .path [at ] = ppath
105
111
106
112
107
113
def abs_path (value ):
@@ -118,6 +124,22 @@ def abs_path(value):
118
124
return value
119
125
120
126
127
+ def translate_directory (directory , base_executable , exe_dir , exec_prefix , base_prefix , prefix , base_exec_prefix ):
128
+ if dir_starts_with (directory , exe_dir ):
129
+ # content inside the exe folder needs to remap to original executables folder
130
+ orig_exe_folder = base_executable [: base_executable .rfind (sep )]
131
+ return "{}{}" .format (orig_exe_folder , directory [len (exe_dir ) :])
132
+ elif dir_starts_with (directory , prefix ):
133
+ return "{}{}" .format (base_prefix , directory [len (prefix ) :])
134
+ elif dir_starts_with (directory , exec_prefix ):
135
+ return "{}{}" .format (base_exec_prefix , directory [len (exec_prefix ) :])
136
+ return directory
137
+
138
+
139
+ def dir_starts_with (directory , prefix ):
140
+ return directory == prefix or directory .startswith (prefix + sep )
141
+
142
+
121
143
def disable_user_site_package ():
122
144
"""Flip the switch on enable user site package"""
123
145
# sys.flags is a c-extension type, so we cannot monkeypatch it, replace it with a python class to flip it
0 commit comments