惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
Security Latest
Security Latest
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cybersecurity and Infrastructure Security Agency CISA
Cloudbric
Cloudbric
T
Threat Research - Cisco Blogs
大猫的无限游戏
大猫的无限游戏
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
C
Cisco Blogs
V
Vulnerabilities – Threatpost
S
Security Archives - TechRepublic
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
J
Java Code Geeks
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy International News Feed
P
Palo Alto Networks Blog
博客园_首页
V
V2EX
WordPress大学
WordPress大学
Schneier on Security
Schneier on Security
月光博客
月光博客
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
The Cloudflare Blog
罗磊的独立博客
美团技术团队
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Last Week in AI
Last Week in AI
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
S
SegmentFault 最新的问题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog

又见苍岚

COLMAP PatchMatch Stereo 算法详解 事件驱动的状态机框架:从理论到工程实践 Git 在国内网络环境下无法 Push 的排查与修复 —— 配置 Clash 代理 分段五次多项式插值原理详解 路径插值方法深度对比研究 Claude Code 使用指南 OpenClaw 记忆管理与技能创建指南 CBS(Conflict-Based Search)算法详解 A* 算法及其变种详解 OpenClaw 配置多 Agents Windows Powershell 无法加载文件,因为在此系统上禁止运行脚本问题的解决方案 MaxClaw 安装流程 大模型 AI 名词介绍 AList 网盘聚合工具简介 Protobuf 简介与测试 Claude Code 简介以及 GLM 4.7 模型接入 Github 歌词下载工具 163MusicLyrics Python __getattr__ 懒加载 Python TypedDict 机器人仿真平台 Gazebo 安装记录 机器人仿真平台 Gazebo 简介 多机器人路径规划问题(Multi-Agent Path Finding, MAPF)简介 Python exifread 读取修改过的 jpeg 信息错误问题修复 3D 坐标系变换的理解 3D 旋转矩阵基本概念 MongoDB Compass 介绍 Python 环境管理工具 uv Flutter 开发指南 Snipaste 安装下载与黑屏问题解决方案 全局路径规划算法记录 2025 Python 版本性能测试 Flutter Hello World Flutter 安装环境配置 Ubuntu VMware 硬盘扩容后 SMBus Host controller not enabled 报错问题解决 Python NetworkX 教程 Docker GPU 报错 - Failed to initialize NVML Unknown Error 解决方案 Python matplotlib 图表绘制 cuda-toolkit 安装替代 Cuda 与 Cudnn Jinja2 Python 利用 docxtpl 和 Jinja2 生成基于模板的 Word 文档 Docker 实现 CPU 核心隔离 LoFTR 基于 Transformer 的特征提取匹配算法 OmniGlue 特征匹配 SuperGlue 使用图神经网络学习特征匹配 Ubuntu 下将 xlsx 文件按照 sheet 转换为 图片 Python 使用 SQLAlchemy Python FastAPI 教程 openwrt 软路由配置安装 Nav2 地图文件(PGM/YAML)规范标准 3D OBJ 模型转换为 glb 瓦片格式 Python 源码 Redis 数据库介绍 Ubuntu 22.04 内核自动升级导致 MongoDB 7.0.12 错误记录 ubuntu 20.04 安装 ROS Noetic ubuntu 18.04 安装 ROS Melodic VMware Workstation Pro 个人免费版下载、安装、使用指南 Hybrid A-star 路径规划 Reeds-Shepp 曲线 Dubins 曲线 Linux kvm 虚拟机网络不通的问题解决方法 Ubuntu 自动内存清理 BiliBili 缓存视频转 mp4 Python 求解线性规划 3D Gaussian Splatting 官方源码实践记录 ImageMagick 教程 Ubuntu 22.04 安装 Colmap 对数几率 odds Ubuntu nmcli 网络管理工具使用指南 SuperPoint 自监督深度学习特征点提取 SyncTV Music Tag Web 在线音乐信息整理工具 ncm 格式转 mp3 MusicBrainz 音乐元数据百科数据库 Ubuntu 网络流量监控工具 私人云音乐平台 Navidrome 入门 手眼标定 四元数(Quaternions) OHTTPS 实现免费自动 https 证书申请、更新、部署 ubuntu 22.04 安装 CloudCompare 单机 KVM 虚拟机冷迁移 Ubuntu 22.04 使用 mdadm 实现软 raid 小鱼 一键安装 ROS-humble Fluid -46- 基于 Simpletex API 构建公式识别页面 公式识别 API 简介 -- Simpletex 使用 Python web 部署库 waitress 3D Gaussian Splatting for Real-Time Radiance Field Rendering Ubuntu Swap 简介与空间扩展 Ubuntu 24.04 安装 forticlient Clash Verge 使用 MongoDB 7.0.17 集群 Docker 构建源码 Error code - 2013. Lost connection to MySQL server during query 问题解决 Python 日志记录库 loguru 使用指北 Python 实现 Web 日志查看服务 MySQL LOAD DATA LOCAL INFILE 极速数据加载 Image size exceeds limit of 89478485 pixels 解决方案 Docker 使用 NVIDIA GPU 驱动错误解决 阿里云 docker 镜像仓库 Ubuntu中没有wired connected的解决方案 MinIO 简介 subconverter 代理订阅格式转换 修复 node –openssl-legacy-provider is not allowed in NODE_OPTIONS 错误
Python 包懒加载
Yiwei Zhang · 2024-11-11 · via 又见苍岚

在Python中,模块导入通常是在程序开始时进行的,这意味着一旦模块被导入,其相关的代码和资源就会被加载到内存中,直到程序结束。尽管这在大多数情况下都能正常工作,但在处理大型项目或具有复杂依赖关系的代码时,这种“早期导入”的方式可能会导致不必要的内存使用和启动延迟。本文记录 Python 在 import 时常用的懒加载实现方案。

简介

为了解决这个问题,Python提供了惰性导入的概念,即在真正需要时才导入模块。这可以通过几种方式实现,包括使用importlib模块、使用__import__函数、以及使用延迟执行的技术(如lambda函数或装饰器)。

_LazyModule

https://github.com/huggingface/diffusers

huggingface 包的组织方式中出现了_LazyModule,这个模块实际上对应了python中的一种lazy import的思想。也就是在整个包很大的情况下不再将所有的包都import,而仅仅在使用的时候进行真正的import。这么做可以极大地缩短整个的import时间。

核心实现

源码链接: https://github.com/huggingface/diffusers/blob/main/src/diffusers/utils/import_utils.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Inspired by diffusers repo
# https://github.com/huggingface/diffusers/blob/main/src/diffusers/utils/import_utils.py
import os
import importlib.util
from itertools import chain
from types import ModuleType
from typing import Any

class _LazyModule(ModuleType):
"""
Module class that surfaces all objects but only performs associated imports when the objects are requested.
"""

# Very heavily inspired by optuna.integration._IntegrationModule
# https://github.com/optuna/optuna/blob/master/optuna/integration/__init__.py
def __init__(self, name, module_file, import_structure, module_spec=None, extra_objects=None):
super().__init__(name)
self._modules = set(import_structure.keys())
self._class_to_module = {}
for key, values in import_structure.items():
for value in values:
self._class_to_module[value] = key
# Needed for autocompletion in an IDE
self.__all__ = list(import_structure.keys()) + \
list(chain(*import_structure.values()))
self.__file__ = module_file
self.__spec__ = module_spec
self.__path__ = [os.path.dirname(module_file)]
self._objects = {} if extra_objects is None else extra_objects
self._name = name
self._import_structure = import_structure

# Needed for autocompletion in an IDE
def __dir__(self):
result = super().__dir__()
# The elements of self.__all__ that are submodules may or may not be in the dir already, depending on whether
# they have been accessed or not. So we only add the elements of self.__all__ that are not already in the dir.
for attr in self.__all__:
if attr not in result:
result.append(attr)
return result

def __getattr__(self, name: str) -> Any:
if name in self._objects:
return self._objects[name]
if name in self._modules:
value = self._get_module(name)
elif name in self._class_to_module.keys():
module = self._get_module(self._class_to_module[name])
value = getattr(module, name)
else:
raise AttributeError(
f"module {self.__name__} has no attribute {name}")

setattr(self, name, value)
return value

def _get_module(self, module_name: str):
try:
return importlib.import_module("." + module_name, self.__name__)
except Exception as e:
raise RuntimeError(
f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
f" traceback):\n{e}"
) from e

def __reduce__(self):
return (self.__class__, (self._name, self.__file__, self._import_structure))

使用方法

首先看一下最高层的包是如何调用这个 lazy import 类的,也就是package_name下的 __init__.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Only support lazy import for now.

# TODO: support slow import

import sys
__version__ = "0.1"

from .utils import (
_LazyModule
)

_import_structure = {
"pipelines": []
}

_import_structure["pipelines"].extend(
[
"a",
"LayoutDMPipeline"
]
)

sys.modules[__name__] = _LazyModule(
__name__,
globals()["__file__"],
_import_structure,
module_spec=__spec__,
extra_objects={"__version__": __version__},
)

我们首先用了一个字典将我们想要导入的东西包起来,然后一起喂给_LazyModule,最后由_LazyModule传给sys.modules

这个包起来的东西包含了根目录的下级目录,extend的部分是我们最终想要导入的东西。流程是这样:

我们想要一个名为a的东西,那么packge就会去找下级目录找a,如果下级目录能够找到a,那么显然可以直接 from XX import a。但是问题出在下级目录显然也没有 a,下级目录又要到下下级目录中去找,直至找到。我们不妨看看叶子的__init__.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Only support lazy import for now.
# TODO: support slow import

import sys
from ...utils import (
_LazyModule
)

_import_structure = {}
_import_structure["bar"] = ["a"]

sys.modules[__name__] = _LazyModule(
__name__,
globals()["__file__"],
_import_structure,
module_spec=__spec__,
)

总结:

我们通过from XX import a,python有如下操作

从 XX 中要a,XX再找pipelines要a,pipelines再找foo要a,foo再找bar要a,最终要到了a。

这么一来,from XX import a, from XX.pipelines import a, from XX.pipelines.foo import a, from XX.pipelines.foo.bar import a,全部都是可用的。

TensorFlow

Tensorflow 的 LazyLoad 方案:

核心实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Code copied from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/util/lazy_loader.py
"""A LazyLoader class."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import importlib
import types

class LazyLoader(types.ModuleType):
"""Lazily import a module, mainly to avoid pulling in large dependencies.

`contrib`, and `ffmpeg` are examples of modules that are large and not always
needed, and this allows them to only be loaded when they are used.
"""

# The lint error here is incorrect.
def __init__(self, local_name, parent_module_globals, name): # pylint: disable=super-on-old-class
self._local_name = local_name
self._parent_module_globals = parent_module_globals

super(LazyLoader, self).__init__(name)

def _load(self):
# Import the target module and insert it into the parent's namespace
module = importlib.import_module(self.__name__)
self._parent_module_globals[self._local_name] = module

# Update this object's dict so that if someone keeps a reference to the
# LazyLoader, lookups are efficient (__getattr__ is only called on lookups
# that fail).
self.__dict__.update(module.__dict__)

return module

def __getattr__(self, item):
module = self._load()
return getattr(module, item)

def __dir__(self):
module = self._load()
return dir(module)

使用方法

  • 代码说明:

类 LazyLoader 继承自 types.ModuleType,初始化函数确保惰性模块将像真正的模块一样正确添加到全局变量中,只要真正用到模块的时候,也就是执行 getattrdir 时,才会真正的 import 实际模块,更新全局变量以指向实际模块,并且将其所有状态(dict)更新为实际模块的状态,以便对延迟加载的引用,加载模块不需要每次访问都经过加载过程。

  • 代码使用:

正常情况下我们这样导入模块:

1
import tensorflow.contrib as contrib1.

其对应的惰性导入版本如下:

1
contrib = LazyLoader('contrib', globals(), 'tensorflow.contrib')

Python 原生实现

importlib

importlib是Python标准库中的一个模块,提供了动态导入模块的功能。通过importlib.import_module函数,可以在运行时按需导入模块。

1
2
3
4
5
6
import importlib
def lazy_import(module_name):
return importlib.import_module(module_name)
# 当需要使用时才导入模块
numpy = lazy_import('numpy')
array = numpy.array([1, 2, 3])

__import__

__import__是一个内置函数,其行为类似于import语句。它接受模块名作为字符串,并返回导入的模块对象。

1
2
3
4
5
def lazy_import(module_name):
return __import__(module_name)
# 当需要使用时才导入模块
numpy = lazy_import('numpy')
array = numpy.array([1, 2, 3])

lambda函数

另一种实现惰性导入的方法是使用延迟执行的技术,如lambda函数。

1
2
3
numpy = lambda: __import__('numpy')
# 当需要使用时才执行导入
array = numpy().array([1, 2, 3])

装饰器

装饰器是另一种延迟执行技术的实现

1
2
3
4
5
6
7
8
9
10
def lazy_import(module_name):
def wrapper():
globals()[module_name] = __import__(module_name)
return globals()[module_name]
return wrapper
@lazy_import('numpy')
def numpy_function():
return numpy.array([1, 2, 3])
# 当需要使用时才执行导入和函数调用
result = numpy_function()

LazyLoader

importlib.util 中的 LazyLoader 可以懒加载。

目录结构

1
2
3
.
├── a.py
└── impt.py

impt.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import sys
import importlib
from importlib.util import LazyLoader

for i, mp in enumerate(sys.meta_path):
if str(mp) == "<class '_frozen_importlib_external.PathFinder'>":
index = i
path_finder = mp
continue

class LazyPathFinder(object):

def find_spec(self, fullname, path, target=None):
spec = path_finder.find_spec(fullname, path, target=target)
if spec is not None:
spec.loader = LazyLoader(spec.loader)
return spec

sys.meta_path[index] = LazyPathFinder()

import os
import a

print('a lazy imported')
print(a.b)

a.py

1
2
3
print('exec a.py')

b = 2

执行并查看输出结果

1
2
3
4
$ python impt.py 
a lazy imported
exec a.py
2

参考资料

文章链接:
https://www.zywvvd.com/notes/coding/python/python-lazy-load/python-lazy-load/