[BugFix] Fix _has_module to verify native deps via trial import (#44035)

Signed-off-by: esmeetu <jasonailu87@gmail.com>
Signed-off-by: Jeffrey Wang <jeffreywang@anyscale.com>
Signed-off-by: Nick Hill <nickhill123@gmail.com>
Co-authored-by: esmeetu <jasonailu87@gmail.com>
Co-authored-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
Jeffrey Wang
2026-05-31 22:06:33 -07:00
committed by GitHub
co-authored by esmeetu Nick Hill
parent 4721bb3aa4
commit 29d69332aa
2 changed files with 110 additions and 5 deletions
+16 -4
View File
@@ -392,12 +392,24 @@ class LazyLoader(ModuleType):
# Optional dependency detection utilities
@cache
def _has_module(module_name: str) -> bool:
"""Return True if *module_name* can be found in the current environment.
"""Return True if *module_name* can be imported in the current environment.
The result is cached so that subsequent queries for the same module incur
no additional overhead.
Uses ``importlib.util.find_spec`` as a fast pre-check, then performs a
trial import to verify that native dependencies (shared libraries, etc.)
are also satisfied. Any failure during the trial import is treated as the
module being unavailable. The result is cached so that subsequent queries
for the same module incur no additional overhead.
"""
return importlib.util.find_spec(module_name) is not None
try:
if importlib.util.find_spec(module_name) is None:
return False
importlib.import_module(module_name)
except ImportError:
logger.warning(
"Module %s was found but failed to import", module_name, exc_info=True
)
return False
return True
def has_deep_ep() -> bool: