Python importlib 动态加载
文章目录
- 1. importlib 库 概述
- 2. 导入模块(import_module())
- 2.1. 导入已安装的模块
- 2.2. 导入子模块
- 2.3 通过字符串变量导入模块
- 3. 重新加载模块(reload())
- 4. 检查模块是否存在(find_spec)
- 5. 获取模块路径(find_spec().origin)
- 6. 加载 .py 文件为模块(spec_from_file_location)
- 7. 读取模块资源(importlib.resources)
- 8. 获取模块元数据(importlib.metadata)
1. importlib 库 概述
importlib 是 Python 的动态导入模块,用于运行时加载模块、重新加载模块、自定义模块导入,适用于 插件系统、动态代码执行等场景。
importlib 主要功能
动态导入模块(import_module)
重新加载已导入的模块(reload)
加载 .py 文件为模块(spec_from_file_location)
检查模块是否存在 util.find_spec()
获取模块路径 util.find_spec().origin
importlib.util.spec_from_loader(name, loader, *, origin=None):
这个函数用于创建一个模块规范对象,基于给定的模块名称、加载器对象和可选的模块来源。它可以用于动态加载由自定义加载器提供的模块。
官方:
https://docs.python.org/zh-cn/3/library/importlib.html
参考:
https://blog.csdn.net/u013172930/article/details/146214799
typing.cast:
https://blog.csdn.net/engchina/article/details/144367018
2. 导入模块(import_module())
2.1. 导入已安装的模块
import importlib# 运行时导入模块
math_module = importlib.import_module("math")# 调用模块中的函数
print(math_module.sqrt(16)) # 4.0
等价于
import math
print(math.sqrt(16)) # 4.0
import_module(“math”) 在运行时导入 math。
2.2. 导入子模块
import importlibjson_encoder = importlib.import_module("json.encoder")
print(json_encoder.JSONEncoder) # <class 'json.encoder.JSONEncoder'>
导入 json.encoder,无需 import json。
等价于
import json
print(json.encoder.JSONEncoder) # <class 'json.encoder.JSONEncoder'>
2.3 通过字符串变量导入模块
import importlibmodule_name = "random"
random_module = importlib.import_module(module_name)
print(random_module.randint(1, 10)) # 生成随机数
等价于
import random
print(random.randint(0, 10)) # 生成随机数
可用于插件系统,动态加载模块。
3. 重新加载模块(reload())
import importlib
import mymodule # 假设 `mymodule.py` 已导入importlib.reload(mymodule) # 重新加载模块
reload() 用于修改代码后立即生效(适用于开发环境)。
4. 检查模块是否存在(find_spec)
importlib.util.find_spec(name, package=None):
这个函数用于查找指定名称的模块规范对象。它会搜索 sys.path 中的目录和 zip 文件,返回一个 ModuleSpec 对象,如果找不到则返回 None。
import importlib.utilspec = importlib.util.find_spec("numpy")
if spec:module = importlib.util.module_from_spec(spec)spec.loader.exec_module(module) # 加载模块print(module.array([1, 2, 3])) # 使用 numpy.array
5. 获取模块路径(find_spec().origin)
from importlib.util import find_specspec = find_spec("torch")
if spec:print(f"torch 的位置: {spec.origin}")
6. 加载 .py 文件为模块(spec_from_file_location)
如果 .py 文件不在 Python sys.path 中,不能直接 import,但可以用 importlib 加载:
importlib.util.module_from_spec(spec):
这个函数用于创建一个新的模块对象,基于给定的模块规范对象 spec。返回的模块对象可以通过 sys.modules 进行访问。
importlib.util.spec_from_file_location(name, location, *, loader=None, submodule_search_locations=None):
这个函数用于创建一个模块规范对象,基于给定的模块名称、模块文件路径和可选的加载器对象。它可以用于动态加载位于特定位置的模块。
importlib.abc.loader.exec_module(module)
运行模块代码。
首先、创建文件 “utils/testmodel.py”:
def say_hello():print("hello world")
然后、加载 “utils/testmodel.py” 为模块:
import importlib.util
import sysdef test_model(module_name, file_path):existed_spec = importlib.util.find_spec(module_name)if existed_spec:print(f"module {module_name} existed_spec")spec = existed_specif not spec.loader:raise Exception(f"Failed to load module {module_name} from {file_path!r}")else:print(f"module {module_name} not_existed_spec")# 创建模块 specspec = importlib.util.spec_from_file_location("testmodule", file_path)if not spec or not spec.loader:raise Exception(f"Failed to load module {module_name} from {file_path!r}")module = importlib.util.module_from_spec(spec)# 添加到系统模块if not existed_spec:sys.modules[module_name] = module# 执行模块代码spec.loader.exec_module(module)return moduleif __name__ == "__main__":module_name = "testmodule"# 指定文件路径# file_path = "/utils/testmodel.py"file_path = "utils/testmodel.py"module = test_model(module_name, file_path)module.say_hello()# 查看系统模块是否已经更新existed_spec = importlib.util.find_spec(module_name)if existed_spec:print(f"module {module_name} existed_spec {existed_spec.origin}")
运行结果:
> python.exe .\test.py
module testmodule not_existed_spec
hello world
module testmodule existed_spec D:\works\demo\importlb\utils/testmodel.py
7. 读取模块资源(importlib.resources)
import importlib.resources# # 读取 mypackage 包内的 data.txt 文件
# with importlib.resources.open_text("mypackage", "data.txt") as f:
with importlib.resources.open_text("requests", "__version__.py") as f:content = f.read()print(content)
运行结果:
> python.exe .\test.py
module testmodule not_existed_spec
hello world
module testmodule existed_spec D:\works\demo\importlb\utils/testmodel.py
# .-. .-. .-. . . .-. .-. .-. .-.
# |( |- |.| | | |- `-. | `-.
# ' ' `-' `-`.`-' `-' `-' ' `-'__title__ = "requests"
__description__ = "Python HTTP for Humans."
__url__ = "https://requests.readthedocs.io"
__version__ = "2.32.3"
__build__ = 0x023203
__author__ = "Kenneth Reitz"
__author_email__ = "me@kennethreitz.org"
__license__ = "Apache-2.0"
__copyright__ = "Copyright Kenneth Reitz"
__cake__ = "\u2728 \U0001f370 \u2728"
8. 获取模块元数据(importlib.metadata)
import importlib.metadataprint(importlib.metadata.version("requests")) # 2.32.3 (获取 `requests` 版本)