Python dynamically imports objects, importlib.import_ Module() uses

Background of

  • a function needs to be run by dynamically importing the corresponding configuration file according to the configuration of different projects.
solution

  • file structure
a #文件夹
	│a.py
	│__init__.py
b #文件夹
	│b.py
	│__init__.py
	├─c#文件夹
		│c.py
		│__init__.py

# c.py 中内容
args = {'a':1}

class C:
    
    def c(self):
        pass

Purpose of the

  • to a module import c.p y objects in the

  • solution

    a.py

    
    import importlib
    
    params = importlib.import_module('b.c.c') #绝对导入
    params_ = importlib.import_module('.c.c',package='b') #相对导入
    
    # 对象中取出需要的对象
    params.args #取出变量
    params.C  #取出class C
    params.C.c  #取出class C 中的c 方法
    

    import_module

Read More: