There is a modules directory with an empty __init__.py file and subdirectories with __init__.py in each. I can not immediately make an explicit import when developing an application, since I do not know how they will call subdirectories. How to import modules in an already running application from subdirectories?

 +--+modules + +--+__init__.py | +--+unknown2 | + | +--+__init__.py | +--+unknown3 | + | +--+__init__.py | +--+unknown1 | +--+__init__.py 

1 answer 1

To import all child modules from the package modules :

 #!/usr/bin/env python3 import pkgutil import modules for importer, modname, ispkg in pkgutil.iter_modules(modules.__path__): # import a module given its name and the importer module = importer.find_module(modname).load_module() assert module.__name__ == modname