diff --git a/pelican/plugins/_utils.py b/pelican/plugins/_utils.py index 4e6ec3c5..ffe32799 100644 --- a/pelican/plugins/_utils.py +++ b/pelican/plugins/_utils.py @@ -53,6 +53,9 @@ def load_legacy_plugin(plugin, plugin_paths): if spec is None: raise ImportError('Cannot import plugin `{}`'.format(plugin)) else: + # Avoid loading the same plugin twice + if spec.name in sys.modules: + return sys.modules[spec.name] # create module object from spec mod = importlib.util.module_from_spec(spec) # place it into sys.modules cache diff --git a/pelican/tests/test_plugins.py b/pelican/tests/test_plugins.py index 06940884..29729539 100644 --- a/pelican/tests/test_plugins.py +++ b/pelican/tests/test_plugins.py @@ -131,6 +131,17 @@ class PluginTest(unittest.TestCase): 'normal subpackage plugin'}, get_plugin_names(plugins)) + # ensure normal plugins are loaded only once + SETTINGS = { + 'PLUGINS': ['normal_plugin'], + 'PLUGIN_PATHS': [self._NORMAL_PLUGIN_FOLDER], + } + plugins = load_plugins(SETTINGS) + for plugin in load_plugins(SETTINGS): + # The second load_plugins() should return the same plugin + # objects as the first one + self.assertIn(plugin, plugins) + # namespace plugin short SETTINGS = { 'PLUGINS': ['ns_plugin']