Skip to content

Fix missing importlib.util import in exact_search.py#207

Open
peylix wants to merge 1 commit intobeir-cellar:mainfrom
peylix:fix/importlib-util
Open

Fix missing importlib.util import in exact_search.py#207
peylix wants to merge 1 commit intobeir-cellar:mainfrom
peylix:fix/importlib-util

Conversation

@peylix
Copy link

@peylix peylix commented Dec 12, 2025

Description

This PR fixes an AttributeError: module 'importlib' has no attribute 'util' that occurs when importing DenseRetrievalExactSearch.

Problem

Previously when using DenseRetrievalExactSearch, I encountered the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "beir/retrieval/search/dense/__init__.py", line 3, in <module>
    from .exact_search import DenseRetrievalExactSearch
  File "beir/retrieval/search/dense/exact_search.py", line 8, in <module>
    if importlib.util.find_spec("faiss") is not None:
       ^^^^^^^^^^^^^^
AttributeError: module 'importlib' has no attribute 'util'

After investigation, I found the file beir/retrieval/search/dense/exact_search.py currently imports only the importlib module but attempts to use importlib.util.find_spec(). In Python, importing a parent module does not automatically import its submodules, leading to an AttributeError.

Solution

Explicitly import the importlib.util submodule:

import importlib.util

This is consistent with other files in the codebase (e.g., faiss_search.py, faiss_index.py) that already use the correct import.

Test Environment

  • Python: 3.12
  • BEIR: 2.2.0+
  • OS: macOS

The module was using `importlib.util.find_spec()` without properly
importing the submodule, which could cause an AttributeError. This
commit changed `import importlib` to `import importlib.util` to avoid
the error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant