Skip to content

Instantly share code, notes, and snippets.

@iuridiniz
Created December 1, 2022 20:16
Show Gist options
  • Save iuridiniz/0975f38a2aa1ed123c2ce2fa2a6edcb8 to your computer and use it in GitHub Desktop.
Save iuridiniz/0975f38a2aa1ed123c2ce2fa2a6edcb8 to your computer and use it in GitHub Desktop.
import all modules from this directory (recursively) to register them
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