def import_pkg()

in tzrec/utils/load_class.py [0:0]


def import_pkg(pkg_info, prefix_to_remove=None):
    """Import package.

    Args:
        pkg_info: pkgutil.ModuleInfo object
        prefix_to_remove: the package prefix to be removed
    """
    package_path = pkg_info[0].path
    if prefix_to_remove is not None:
        package_path = package_path.replace(prefix_to_remove, "")
    mod_name = pkg_info[1]

    if package_path.startswith("/"):
        # absolute path file, we should use relative import
        mod = pkg_info[0].find_module(mod_name)
        if mod is not None:
            # skip those test files
            if not mod_name.endswith("_test"):
                mod.load_module(pkg_info[1])
        else:
            raise Exception("import module %s failed" % (package_path + mod_name))
    else:
        # use similar import methods as the import keyword
        module_path = os.path.join(package_path, mod_name).replace("/", ".")
        # skip those test files
        if not mod_name.endswith("_test"):
            try:
                __import__(module_path)
            except Exception as e:
                raise ValueError(
                    "import module %s failed: %s" % (module_path, str(e))
                ) from e