From 9bbadddfa9e3a02408ba736b7083e13ae57c791e Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sat, 20 Feb 2021 11:09:16 +0800 Subject: [PATCH] :bug: fix sub plugin relative import --- nonebot/plugin/manager.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/nonebot/plugin/manager.py b/nonebot/plugin/manager.py index 7bdf0910..a184d956 100644 --- a/nonebot/plugin/manager.py +++ b/nonebot/plugin/manager.py @@ -35,9 +35,8 @@ class _NamespaceModule(ModuleType): class _InternalModule(ModuleType): """Internal module for each plugin manager.""" - def __init__(self, plugin_manager: "PluginManager"): - super().__init__( - f"{_internal_space.__name__}.{plugin_manager.internal_id}") + def __init__(self, prefix: str, plugin_manager: "PluginManager"): + super().__init__(f"{prefix}.{plugin_manager.internal_id}") self.__plugin_manager__ = plugin_manager @property @@ -88,7 +87,12 @@ class PluginManager: def _setup_internal_module(self, internal_id: str) -> ModuleType: if hasattr(_internal_space, internal_id): raise RuntimeError("Plugin manager already exists!") - module = _InternalModule(self) + + prefix = sys._getframe(2).f_globals.get( + "__name__") or _internal_space.__name__ + if not prefix.startswith(_internal_space.__name__): + prefix = _internal_space.__name__ + module = _InternalModule(prefix, self) sys.modules[module.__name__] = module setattr(_internal_space, internal_id, module) return module