fix: 为 Runtime 和 Loader 补充包式插件导入支持

This commit is contained in:
anderwer
2026-03-15 16:58:53 +08:00
parent e1b3cf6e9f
commit e8244ed49e
2 changed files with 22 additions and 4 deletions

View File

@@ -9,6 +9,7 @@
from collections import deque
from typing import Any, Dict, List, Optional, Set, Tuple
import contextlib
import importlib
import importlib.util
import json
@@ -239,7 +240,19 @@ class PluginLoader:
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
plugin_parent_dir = os.path.dirname(plugin_dir)
inserted_plugin_parent = False
if plugin_parent_dir and plugin_parent_dir not in sys.path:
sys.path.insert(0, plugin_parent_dir)
inserted_plugin_parent = True
try:
spec.loader.exec_module(module)
finally:
if inserted_plugin_parent:
with contextlib.suppress(ValueError):
sys.path.remove(plugin_parent_dir)
# 优先使用新版 create_plugin 工厂函数
create_plugin = getattr(module, "create_plugin", None)