首页 > 其他 > 详细

模块动态导入

时间:2021-05-31 15:56:58      阅读:10      评论:0      收藏:0      [点我收藏+]
# def module_resolver(namespace):
#     namespace_parts = namespace.split(".")
#     module_name = ".".join(namespace_parts[0:-1])
#     cls_name = namespace_parts[-1]
#     try:
#         module = __import__(module_name, fromlist=["*"])
#         if hasattr(module, cls_name):
#             return getattr(module, cls_name)
#     except Exception as ex:
#         app_log.error("resolve %s failed with exception %s" % (namespace, ex))

# 建议使用方式二动态导入模块
def module_resolver(namespace):
    from importlib import import_module
    namespace_parts = namespace.split(".")
    module_name = ".".join(namespace_parts[0:-1])
    cls_name = namespace_parts[-1]
    try:
        module = import_module(module_name)
        if hasattr(module, cls_name):
            return getattr(module_name, cls_name)
    except Exception as ex:
        app_log.error("resolve %s failed with exception %s" % (namespace, ex))

 

模块动态导入

原文:https://www.cnblogs.com/52-qq/p/14830799.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!