|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import importlib.util |
| 4 | +import sys |
4 | 5 | from itertools import chain |
5 | 6 | from pathlib import Path |
6 | 7 | from typing import ( |
@@ -80,15 +81,21 @@ def _import_from_file_impl( |
80 | 81 | source: FileSource, callable_name: str, **kwargs: Unpack[HashKwargs] |
81 | 82 | ): |
82 | 83 | 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"? |
89 | 98 |
|
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"? |
92 | 99 | return getattr(dep, callable_name) |
93 | 100 |
|
94 | 101 |
|
|
0 commit comments