I have a folder of modules (called modules) that currently contains 3 folders, each of which contain a package with an __init__.py
file.
What I would like to do is iterate through the modules folder, import each of the packages and execute its main()
function.
I have attempted to do so like this:
import os
for dirnames in os.walk('modules'):
for subdirname in dirnames:
__import__("modules", subdirname)
func = getattr(subdirname, "main", None)
if func:
func()
but it doesn't seem to be importing the modules, let alone executing the function.
I tried adding a manual call but it returned an error:
Traceback (most recent call last):
File "test.py", line 12, in <module>
rfm_433mhz.main()
NameError: name 'rfm_433mhz' is not defined
EDIT: I have added the changes as suggested but am still getting an AttributeError:
Module: rfm_433mhz
Traceback (most recent call last):
File "test.py", line 9, in <module>
func = getattr(module, "main")
AttributeError: 'module' object has no attribute 'main'
For testing the __init__.py
file has:
def main():
print "hello"