Reputation: 7822
I setup this complex structure to load submodules dynamically:
Main.py
src \ Clean.py
Source \ Amazon \ Clean.py
Source \ Google \ Clean.py
Source \ Amazon \ __init__.py
Source \ Google \ __init__.py
My src \ Clean.py look like this:
import importlib
Vendors = ["Amazon","Google"]
def Run(Config):
results = ""
for vendor in Vendors:
mod = importlib.import_module("source."+vendor)
results += mod.Run()
return results
My __init__.py look like this:
__all__ = ["clean"]
My source>>amazon>>clean.py look like this:
def Run():
return "Amazon Clean Test"
When I call src \ clean.py, I get this error:
AttributeError: module 'source.Amazon' has no attribute 'Run'
I am calling the whole thing in Main.py:
import src.clean as Clean
result = Clean.Run(Config)
I guess it couldn't load the module in source \ amazon \ clean.py? How do I load that?
Stacktrace:
File "main.py", line 2, in clean
result = Clean.Run(Config)
File "src\clean.py", line 7, in Run
results += mod.Run()
AttributeError: module 'source.Amazon' has no attribute 'Run'
Upvotes: 0
Views: 394
Reputation: 7822
Got it to work!
In src \ clean.py, change it dynamic import line to this:
mod = importlib.import_module("source."+vendor+".clean")
Delete __init__.py
That's it!
Upvotes: 1