[core/module] add fallback for module loading

Looks like some Python versions work with find_spec(), others with
spec_from_file_location(), so add find_spec() as fallback.

fixes #779
This commit is contained in:
tobi-wan-kenobi 2021-04-02 03:30:09 +00:00
parent 7f2ce7d76e
commit 527d1706c2

View file

@ -27,10 +27,16 @@ def import_user(module_short, config, theme):
return getattr(mod, "Module")(config, theme) return getattr(mod, "Module")(config, theme)
else: else:
log.debug("importing {} from user via importlib.util".format(module_short)) log.debug("importing {} from user via importlib.util".format(module_short))
try:
spec = importlib.util.spec_from_file_location("modules.{}".format(module_short), usermod) spec = importlib.util.spec_from_file_location("modules.{}".format(module_short), usermod)
mod = importlib.util.module_from_spec(spec) mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod) spec.loader.exec_module(mod)
return mod.Module(config, theme) return mod.Module(config, theme)
except Exception as e:
spec = importlib.util.find_spec("modules.{}".format(module_short), usermod)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod.Module(config, theme)
raise ImportError("not found") raise ImportError("not found")
"""Loads a module by name """Loads a module by name