Created
December 1, 2022 20:16
-
-
Save iuridiniz/0975f38a2aa1ed123c2ce2fa2a6edcb8 to your computer and use it in GitHub Desktop.
import all modules from this directory (recursively) to register them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import importlib | |
import os.path | |
import pkgutil | |
def import_all_modules(path, package): | |
# import all modules from this directory (recursively) to register endpoints in api_blueprint | |
# print("++importing all modules from: " + path) | |
pkgs = [] | |
for (_, module_name, ispkg) in pkgutil.iter_modules([path]): | |
# print(f"importing module {module_name!r} in path {path!r} (ispkg={ispkg})") | |
importlib.import_module("." + module_name, package=package) | |
if ispkg: | |
pkgs += [(os.path.join(path, module_name), package + "." + module_name)] | |
for (pth, pkg) in pkgs: | |
import_all_modules(pth, pkg) | |
import_all_modules(os.path.dirname(__file__), __name__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment