I'd like to dynamically load all modules placed into a given directory (modules
) and run a specific method in each of them (.main()
).
I ran across the following question: How to load all modules in a folder?
and ended up with the following code:
my main.py
import modules
if __name__ == "__main__":
for module in modules.__all__:
module.main()
The __init__.py
in the modules
folder look like this:
from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
However, I get the below error:
'str' object has no attribute 'main'
I believe this is some kind of newbie question, but I cannot figure out how to do things.