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

推荐订阅源

爱范儿
爱范儿
腾讯CDC
博客园 - 司徒正美
A
About on SuperTechFans
H
Help Net Security
J
Java Code Geeks
C
Check Point Blog
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
MyScale Blog
MyScale Blog
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
博客园 - 【当耐特】
雷峰网
雷峰网

博客园 - 荣锋亮

just-git 纯js 的git 实现 阿里开源的UnifiedModel agno agentos interfaces 简单说明 agno agentos ag-ui 协议 agno agentos 暴露a2a 协议 agno gateway模式 agno 多agent 框架集成能力 agno agentos 简单说明 agno workflow 模式简单说明 agno team agent 简单说明 agno remote agent 简单说明 agno agent 使用 agno agent 平台 sshpiper 简单试用 sshpiper ssh 的反向代理服务 context-propagation spring reactor 的上下文传递包 nginx 发布1.31.2 了 reductstore 的一些部署模式 reductstore 数据写入模式 reductstore bridge 方便reductstore数据bridge 扩展 reductstore zenoh 集成 zenoh 1.9.x 一些新特性 ladybugdb嵌入式图数据库 BlockHound 检测 reactor阻塞调用的agent reductstore 高性能面向机器人以及IOT场景的存储以及流数据基石 zerofs 一些新功能 java nats request-many 支持的一些模式 java nats RequestMany context7 面向llm 以及ai代码编辑器的依赖包更新平台 NATS Agents Protocol nats 团队提出的agent 通信协议
python 动态加载模块
荣锋亮 · 2026-04-07 · via 博客园 - 荣锋亮

动态加载模块在插件化中很重要,但是在agent 周边也是比较重要的,比如我们基于ai 生成代码,同时需要动态加载执行代码模块,python 内置了不少方法以下简单说明下

通过importlib.import_module

注意此模式会加载到sys.modules 中,会有cache的问题,同时会有sys.path 查找的问题,可以通过importlib.reload 进行重新加载reload

  • 参考玩法
module = importlib.import_module(f"mymodules_{runner_id}.app")
importlib.reload(module)

通过importlib.util.spec_from_file_location

此模式就比较自由了,路径可以自己指定,每次都是新的module,同时不会cache 到sys.modules中

  • 参考玩法
spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

说明

以上是动态加载的方法,使用好了价值还是比较大的,但是注意动态加载是不进行隔离的,实现加载模块以及依赖(通过uv ),可以参考如下

  • importlib.util.spec_from_file_location 模式
def load_module_from_path(module_name, path):
    try:
        spec = importlib.util.spec_from_file_location(module_name, path)
        module = importlib.util.module_from_spec(spec)
        # install dependencies if requirements.txt exists
        requirements_path = os.path.join(os.path.dirname(path), "requirements.txt")
        if os.path.exists(requirements_path):
            import subprocess
            subprocess.check_call(["uv", "pip", "install", "-r", requirements_path])
        spec.loader.exec_module(module)
        return module
    except ModuleNotFoundError as e:
        print(f"Error loading module {module_name} from path {path}: {e}")
       
  • importlib.import_module
def get_module_dir(module_name):
    spec = importlib.util.find_spec(module_name)
    if spec is None:
        raise ModuleNotFoundError(module_name)

    # package
    if spec.submodule_search_locations:
        return list(spec.submodule_search_locations)[0]

    # 普通模块
    return os.path.dirname(spec.origin)

def load_module_from_module_with_cache(module_name, *args, **kwargs):
    try:
        requirements_path = os.path.join(get_module_dir(f"plugins.{module_name}"), "requirements.txt")
        if os.path.exists(requirements_path):
            import subprocess
            subprocess.check_call(["uv", "pip", "install", "-r", requirements_path])
        return importlib.import_module(f"plugins.{module_name}.app")
    except ModuleNotFoundError:
        print(f"Module plugins.{module_name} not found, loading from path")
        raise ModuleNotFoundError(f"Module plugins.{module_name} not found, loading from path")

参考资料

https://docs.python.org/3/library/importlib.html

https://docs.python.org/3/library/importlib.html#module-importlib.util