Skip to content

Commit 5b7a19b

Browse files
committed
add importlib.reload
1 parent ea65ead commit 5b7a19b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

bioimageio/core/digest_spec.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import importlib.util
4+
import sys
45
from itertools import chain
56
from pathlib import Path
67
from typing import (
@@ -80,15 +81,21 @@ def _import_from_file_impl(
8081
source: FileSource, callable_name: str, **kwargs: Unpack[HashKwargs]
8182
):
8283
local_file = download(source, **kwargs)
83-
module_name = local_file.path.stem
84-
importlib_spec = importlib.util.spec_from_file_location(
85-
module_name, local_file.path
86-
)
87-
if importlib_spec is None:
88-
raise ImportError(f"Failed to import {module_name} from {source}.")
84+
module_name = local_file.path.stem.replace("-", "_").replace(" ", "_")
85+
if module_name in sys.modules:
86+
logger.info(f"attempting to reload previously loaded '{module_name}'")
87+
dep = sys.modules[module_name]
88+
dep = importlib.reload(dep) # reload in case source file has changed
89+
else:
90+
importlib_spec = importlib.util.spec_from_file_location(
91+
module_name, local_file.path
92+
)
93+
if importlib_spec is None:
94+
raise ImportError(f"Failed to import {module_name} from {source}.")
95+
96+
dep = importlib.util.module_from_spec(importlib_spec)
97+
importlib_spec.loader.exec_module(dep) # type: ignore # todo: possible to use "loader.load_module"?
8998

90-
dep = importlib.util.module_from_spec(importlib_spec)
91-
importlib_spec.loader.exec_module(dep) # type: ignore # todo: possible to use "loader.load_module"?
9299
return getattr(dep, callable_name)
93100

94101

0 commit comments

Comments
 (0)