1 from inspect import isclass
2 from pkgutil import iter_modules
3 from pathlib import Path
4 from importlib import import_module
6 # iterate through the modules in the current package
7 PACKAGE_DIR = Path(__file__).resolve().parent
8 MODULES = [module_name for (_, module_name, _) in iter_modules([PACKAGE_DIR])]
10 for module_name in MODULES:
11 # import the module and iterate through its attributes
12 module = import_module(f"{__name__}.{module_name}")
13 for attribute_name in dir(module):
14 attribute = getattr(module, attribute_name)
16 if isclass(attribute):
17 # Add the class to this package's variables
18 globals()[attribute_name] = attribute
20 # cleanup temp helpers
21 del module_name, module, attribute_name, attribute, isclass, iter_modules, Path, import_module