在 python 中执行这种类型的导入的最佳方法是什么
要导入的文件位于位置 one/ne_one/one_two/"
文件名:两个.py
def foo():
print "venkatttt!"
主文件:main.py
s = __import__("one.one_one.one_two.two", fromlist=[])
function_class = getattr(s,"one_one")
function_class1 = getattr(function_class,"one_two")
function_class2 = getattr(function_class1,"two")
print s
print function_class
print function_class1
print function_class2
function_class2.foo()
此代码的输出:
<module 'one' from '/opt/auto/src/ex/one/__init__.pyc'>
<module 'one.one_one' from '/opt/auto/src/ex/one/one_one/__init__.pyc'>
<module 'one.one_one.one_two' from '/opt/auto/src/ex/one/one_one/one_two/__init__.pyc'>
<module 'one.one_one.one_two.two' from '/opt/auto/src/ex/one/one_one/one_two/two.py'>
venkatttt!
我正在寻找执行此导入的最佳方法