Fix 'module not found' for running anything (#27)

* Fix 'module not found' for running anything

* Make sure actual Python modules still function
This commit is contained in:
Benjamin
2023-01-16 13:19:20 -06:00
committed by GitHub
parent 535dffd01a
commit 7b6a4efa77

View File

@@ -87,7 +87,15 @@ def import_from_string(import_str):
raise ImportError("Import string should be in the format <module>:<attribute>")
try:
module = importlib.import_module(module_str)
relpath = f"{module_str}.py"
if os.path.isfile(relpath):
spec = importlib.util.spec_from_file_location(module_str, relpath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
else:
module = importlib.import_module(module_str)
for attr_str in attrs_str.split("."):
module = getattr(module, attr_str)
except AttributeError: