def test_importmode_importlib_with_pickle(
        self, tmp_path: Path, ns_param: bool
    ) -> None:
        """Ensure that importlib mode works with pickle (#7859)."""
        fn = tmp_path.joinpath("_src/tests/test_pickle.py")
        fn.parent.mkdir(parents=True)
        fn.write_text(
            dedent(
                """
                import pickle

                def _action():
                    return 42

                def round_trip():
                    s = pickle.dumps(_action)
                    return pickle.loads(s)
                """
            ),
            encoding="utf-8",
        )

        module = import_path(
            fn, mode="importlib", root=tmp_path, consider_namespace_packages=ns_param
        )
        round_trip = getattr(module, "round_trip")
        action = round_trip()
        assert action() == 42

        # Ensure we do not import the same module again (#11475).
        module2 = import_path(
            fn, mode="importlib", root=tmp_path, consider_namespace_packages=ns_param
        )
        assert module is module2
Ensure that importlib mode works with pickle (#7859).
testing/test_pathlib.py