python根据字符串导入模块
问题:
path = "auth.my_auth.AUTH" # 根据path实例化AUTH类
解决:
path = "auth.my_auth.AUTH" import importlib model_path, class_name = path.rsplit(".", 1) model = importlib.import_module(model_path) # 根据"auth.my_auth"导入my_auth模块 obj = getattr(model, class_name)() # 反射并实例化
诠释了Django中间件等一些了通过字符串导入相应模块的问题