I want to load python(2.7 not 3.x) modules from source code dynamically using imp.load_source
and if I dont't append the source code path to sys.path
the local import can't work. In this case, How to resolve the conflict module name via local import?
I have been search a while and can't find a solution for this. Any ideas? Thanks!
The file structure what I have like this:
/tmp
|---runner
| |-runner.py
/tmp/modules
|---bundle1
| |--bundle
| | |-__init__.py
| | |-index.py
| |-main.py
|
|---bundle2
|--bundle
| |-__init__.py
| |-index.py
|-main.py
In runner.py
import os
import imp
import sys
sys.path.append("/tmp/modules/bundle1")
module_a = imp.load_source("Bundle1", "/tmp/modules/bundle1/main.py")
sys.path.append("/tmp/modules/bundle2")
module_b = imp.load_source("Bundle2", "/tmp/modules/bundle2/main.py")
module_a.start()
module_b.start()
In bundle1/main.py
from bundle import index
def start():
index.a_func()
if __name__ == "__main__":
start()
In bundle2/main.py
from bundle import index
def start():
index.b_func()
if __name__ == "__main__":
start()
Because the conflict of from bundle
, b_func()
can't be found in bundle2/main.py