Background of
strong> h5>
- a function needs to be run by dynamically importing the corresponding configuration file according to the configuration of different projects.
solution strong> h5>
- 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 p> li>
-
solution p> li> ul>
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
- 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 p> li>
-
solution p> li> ul>
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