-
-
Save nooperpudd/02e9a6fe84bf4b58c9f08a74544387e0 to your computer and use it in GitHub Desktop.
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 pkgutil | |
def import_submodules(context, root_module, path): | |
""" | |
Import all submodules and register them in the ``context`` namespace. | |
for examples: | |
>>> import_submodules(locals(), __name__, __path__) | |
:param context: locals() | |
:param root_module: __name__ | |
:param path: __path__ | |
:return: None | |
""" | |
for loader, module_name, is_pkg in pkgutil.walk_packages(path, root_module + '.'): | |
module = loader.find_module(module_name).load_module(module_name) | |
for k, v in vars(module).items(): | |
if not k.startswith('_'): | |
context[k] = v | |
context[module_name] = module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment