# Buggy code
def update_module_paths(cls, new_code_path):
    # Force subsequent imports to come from the .pex directory rather than the .pex file.
    TRACER.log("Adding to the head of sys.path: %s" % new_code_path)
    sys.path.insert(0, new_code_path)
    for name, module in sys.modules.items():
        if hasattr(module, "__path__"):
            module_dir = os.path.join(new_code_path, *name.split("."))
            TRACER.log(
                "Adding to the head of %s.__path__: %s" % (module.__name__, module_dir)
            )
            try:
                module.__path__.insert(0, module_dir)
            except AttributeError:
                # TODO: This is a temporary bandaid for an unhandled AttributeError which results
                # in a startup crash. See https://github.com/pantsbuild/pex/issues/598 for more info.
                TRACER.log(
                    "Failed to insert %s: %s.__path__ of type %s does not support insertion!"
                    % (module_dir, module.__name__, type(module.__path__))
                )


# Traceback (AttributeError)
Traceback (most recent call last):
File ".bootstrap/_pex/pex.py", line 364, in execute
File ".bootstrap/_pex/pex.py", line 90, in _activate
File ".bootstrap/_pex/environment.py", line 147, in activate
File ".bootstrap/_pex/environment.py", line 198, in _activate
File ".bootstrap/_pex/environment.py", line 64, in update_module_paths
AttributeError: '_NamespacePath' object has no attribute 'insert'

# Fixed code
def update_module_paths(cls, pex_file, explode_dir):
    bootstrap = Bootstrap.locate()
    pex_path = os.path.realpath(pex_file)

    # Un-import any modules already loaded from within the .pex file.
    to_reimport = []
    for name, module in reversed(sorted(sys.modules.items())):
        if bootstrap.imported_from_bootstrap(module):
            TRACER.log("Not re-importing module %s from bootstrap." % module, V=3)
            continue

        pkg_path = getattr(module, "__path__", None)
        if pkg_path and any(
            os.path.realpath(path_item).startswith(pex_path) for path_item in pkg_path
        ):
            sys.modules.pop(name)
            to_reimport.append((name, pkg_path, True))
        elif (
            name != "__main__"
        ):  # The __main__ module is special in python and is not re-importable.
            mod_file = getattr(module, "__file__", None)
            if mod_file and os.path.realpath(mod_file).startswith(pex_path):
                sys.modules.pop(name)
                to_reimport.append((name, mod_file, False))

    # Force subsequent imports to come from the exploded .pex directory rather than the .pex file.
    TRACER.log("Adding to the head of sys.path: %s" % explode_dir)
    sys.path.insert(0, explode_dir)

    # And re-import them from the exploded pex.
    for name, existing_path, is_pkg in to_reimport:
        TRACER.log(
            "Re-importing %s %s loaded via %r from exploded pex."
            % ("package" if is_pkg else "module", name, existing_path)
        )
        reimported_module = importlib.import_module(name)
        if is_pkg:
            for path_item in existing_path:
                # NB: It is not guaranteed that __path__ is a list, it may be a PEP-420 namespace package
                # object which supports a limited mutation API; so we append each item individually.
                reimported_module.__path__.append(path_item)