I'm having trouble getting the two following examples to work together, dynamically loading a module and calling a function by string. I'm trying to dynamically load and call python modules.
My file structure is as follows
src/
main.py
__init__.py
modules/
__init__.py
module1.py
module2.py
...
module100.py
In my main.py function I have the following to load the module,
mod = imp.load_source('module', '/path/to/module.py')
This seems to be working fine, print module
yields
<module 'module' from '/path/to/module.py'>
In module.py
I have
class module:
def __init__(self):
print ("HELLO WORLD")
def calc(self):
print ("calc called")
if __name__ == "__main__":
import sys
sys.exit(not main(sys.argv))
The problem is when I try to call the calc function in module,
result = getattr(module, 'calc')()
yields the following
HELLO WORLD
Traceback (most recent call last):
File "main.py", line 39, in main
result = getattr(module, 'calc')()
AttributeError: 'module' object has no attribute 'calc
I'm not sure what i'm missing or doing wrong