MCPcopy
hub / github.com/pallets/werkzeug / find_modules

Function find_modules

src/werkzeug/utils.py:612–642  ·  view source on GitHub ↗

Finds all the modules below a package. This can be useful to automatically import all views / controllers so that their metaclasses / function decorators have a chance to register themselves on the application. Packages are not returned unless `include_packages` is `True`. This ca

(
    import_path: str, include_packages: bool = False, recursive: bool = False
)

Source from the content-addressed store, hash-verified

610
611
612def find_modules(
613 import_path: str, include_packages: bool = False, recursive: bool = False
614) -> t.Iterator[str]:
615 """Finds all the modules below a package. This can be useful to
616 automatically import all views / controllers so that their metaclasses /
617 function decorators have a chance to register themselves on the
618 application.
619
620 Packages are not returned unless `include_packages` is `True`. This can
621 also recursively list modules but in that case it will import all the
622 packages to get the correct load path of that module.
623
624 :param import_path: the dotted name for the package to find child modules.
625 :param include_packages: set to `True` if packages should be returned, too.
626 :param recursive: set to `True` if recursion should happen.
627 :return: generator
628 """
629 module = import_string(import_path)
630 path = getattr(module, "__path__", None)
631 if path is None:
632 raise ValueError(f"{import_path!r} is not a package")
633 basename = f"{module.__name__}."
634 for _importer, modname, ispkg in pkgutil.iter_modules(path):
635 modname = basename + modname
636 if ispkg:
637 if include_packages:
638 yield modname
639 if recursive:
640 yield from find_modules(modname, include_packages, True)
641 else:
642 yield modname
643
644
645class ImportStringError(ImportError):

Callers

nothing calls this directly

Calls 1

import_stringFunction · 0.85

Tested by

no test coverage detected