def _rebuild_mod_path(orig_path, package_name, module):
sys_path = [_normalize_cached(p) for p in sys.path]<EOL>def safe_sys_path_index(entry):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>return sys_path.index(entry)<EOL><DEDENT>except ValueError:<EOL><INDENT>return float('<STR_LIT>')<EOL><DEDENT><DEDENT>def position_in_sys_path(path):<EOL><INDENT>"""<STR_LIT>"""<EOL>path_parts = path.split(os.sep)<EOL>module_parts = package_name.count('<STR_LIT:.>') + <NUM_LIT:1><EOL>parts = path_parts[:-module_parts]<EOL>return safe_sys_path_index(_normalize_cached(os.sep.join(parts)))<EOL><DEDENT>new_path = sorted(orig_path, key=position_in_sys_path)<EOL>new_path = [_normalize_cached(p) for p in new_path]<EOL>if isinstance(module.__path__, list):<EOL><INDENT>module.__path__[:] = new_path<EOL><DEDENT>else:<EOL><INDENT>module.__path__ = new_path<EOL><DEDENT>
Rebuild module.__path__ ensuring that all entries are ordered
corresponding to their sys.path order