lib文件夾下有test.py文件:

test.py文件內容如下:
class simple(object):
def __init__(self):
self.name='剛田武'
在‘動態載入模塊.py’文件下動態載入test.py模塊的方法如下:
module=__import__('lib.test') #此時module相當於lib
print(module)
obj=module.test.simple() #實例化,此時obj相當於lib.test.simple對象
print(obj.name)
輸出為:
<module 'lib' (namespace)> 剛田武
官方推薦用法如下:
import importlib
test=importlib.import_module('lib.test') #此時test相當於test.py文件
print(test)
print(test.simple().name)
輸出:
<module 'lib.test' from 'C:\\Users\\HJJ\\PycharmProjects\\python_learning\\Week8\\lib\\test.py'>
剛田武
