importlib 根据字符串导入模块
应用:
Django中间件,rest framework 组件的全局配置文件
import importlib path = "abc.def.foo" module_path,class_name = path.rsplit('.',maxsplit=1) # 根据字符串的形式导入模块 m = importlib.import_module(module_path) cls = getattr(m,class_name) # foo() obj = cls() obj.process_request()