python学习之-- importlib模块

importlib 模块

Python提供了importlib包作为标准库的一部分。目的就是提供Python中import语句的实现(以及__import__函数)。另外,importlib允许程序员创建他们自定义的对象,可用于引入过程(也称为importer)。

#举例自定义一个模块aa,内部函数为:
class c(object):
def __str__(self):
return 'C language'

# 字符串导入函数2种方法,建议方法
import importlib
aa = importlib.import_module('lib.aa')
c = aa.c()
print(c)

# 第二种方法,已淘汰
lib = __import__('lib.aa')
print(lib)
c = lib.aa.c()
print(c)
原文地址:https://www.cnblogs.com/zy6103/p/7009751.html