Created
April 8, 2018 14:57
-
-
Save crypto-jeronimo/0dfa2be4d4101464d3d5477b8b9fc3ba to your computer and use it in GitHub Desktop.
Dynamically load all Python submodules
This file contains 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, importlib, sklearn, sys | |
def import_submodules(package_name): | |
""" Import all submodules of a module, recursively | |
:param package_name: Package name | |
:type package_name: str | |
:rtype: dict[types.ModuleType] | |
""" | |
package = sys.modules[package_name] | |
modules = {} | |
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__): | |
try: | |
modules[name] = importlib.import_module(package_name + '.' + name) | |
except ModuleNotFoundError: | |
continue | |
return modules |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment