Python中动态导入对象importlib.import_module()的使用

  参考:https://blog.csdn.net/edward_zcl/article/details/88809212

     https://www.cnblogs.com/yhjoker/p/15969508.html

  经常在项目中碰到需要根据配置动态导入不同的类的方法进行运行,这时就要用动态函数import_module的使用方法

  假设项目目录结构如下:

 

  需要在call_module.py文件中动态调用category_100.py和category_102.py中方

  具体调用如下

from importlib import import_module


def test():
    module = import_module('category.category_100')
    func = getattr(module, 'function1')
    func('小明',25)



if __name__=="__main__":
    test()

 

posted on 2023-06-02 16:13  shaomine  阅读(676)  评论(0编辑  收藏  举报