python中根据字符串导入模块module
需要导入importlib,使用其中的import_module方法
import importlib
modname = ‘datetime’
datetime_module = importlib.import_module(modname)
print(datetime_module)
obj = getattr(datetime_module,’datetime’)
print(obj.now())
2019-03-05 23:25:16.077121
print(dir(datetime_module))
[‘MAXYEAR’, ‘MINYEAR’, ‘builtins‘, ‘cached‘, ‘doc‘, ‘file‘, ‘loader‘, ‘name‘, ‘package‘, ‘spec‘, ‘date’, ‘datetime’, ‘datetime_CAPI’, ‘time’, ‘timedelta’, ‘timezone’, ‘tzinfo’]
输出datetime_module即为module对象datetime
通过反射获得其中的属性或方法
调用dir函数查看其中地方属性或方法
来自:https://www.cnblogs.com/sfencs-hcy/p/10480687.html
