Skip to content

Commit

Permalink
fix soft import to exclude local folder (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnth authored Nov 13, 2024
1 parent 61a92fa commit a730b60
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions xinfer/optional_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a730b60

Please sign in to comment.