importlib模块——另一种导入文件的方法

正常我们想要从文件 myfile 导入模块 b 时,我们会使用:

from myfile import b

  

接下来介绍另一种导入方式,能够用字符串来导入模块:

# 首先导入importlib模块
import importlib

res = 'myflie.b'

importlib.import_module(res)  # 就等同于 from myfile import b

  

总结:

  1. 该方法提供了用字符串导入模块的方式:myfile.b

  2.改方法最小只能到py文件名,不能具体到文件内的函数方法

 

posted @ 2023-11-09 19:04  wellplayed  阅读(19)  评论(0编辑  收藏  举报