From 4b3460a18a1ee94cadcf6385af9b69a91a77140f Mon Sep 17 00:00:00 2001 From: dnth Date: Wed, 13 Nov 2024 11:33:36 +0800 Subject: [PATCH] fix soft import to exclude local folder --- xinfer/optional_imports.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/xinfer/optional_imports.py b/xinfer/optional_imports.py index 761f27d..c7eab1e 100644 --- a/xinfer/optional_imports.py +++ b/xinfer/optional_imports.py @@ -3,9 +3,17 @@ def soft_import(name: str): try: - importlib.import_module(name) - return True - except ModuleNotFoundError as e: + module = importlib.import_module(name) + # Verify the module is actually loaded and has a real file path + if ( + module + and hasattr(module, "__file__") + and module.__file__ is not None + and "site-packages" in str(module.__file__) + ): + return True + return False + except (ModuleNotFoundError, ImportError) as e: if str(e) != f"No module named '{name}'": raise e return False