# Unable to import faiss

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-import-faiss%2F3439%3E%5C
**Category:** Beginners\
**Created:** [January 29, 2021, 5:56pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-import-faiss%2F3439 "2021-01-29T17:56:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkserge](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@mkserge](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmkserge)%5C
**Post date:** [January 29, 2021, 5:56pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-import-faiss%2F3439%2F1 "2021-01-29T17:56:17Z")

</div>

Hello,

I am running Transformers on a Mac OS with Python 3.8 in a virtual environment.

I have faiss-cpu 1.7.0 installed in the env.

```bash
(venv) sergey_mkrtchyan transformers (master) $ python
Python 3.8.6 (v3.8.6:db455296be, Sep 23 2020, 13:31:39)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import faiss
>>> faiss. __version__'1.7.0'

```

However transformers repo is doing an additional check of the version of the package using importlib\_metadata in `transformers/src/file_utils.py` which ends up failing on me with “RagRetriever requires the faiss library but it was not found in your environment.”

```auto
_faiss_available = importlib.util.find_spec("faiss") is not None
try:
    _faiss_version = importlib_metadata.version("faiss")
    logger.debug(f"Successfully imported faiss version {_faiss_version}")
except importlib_metadata.PackageNotFoundError:
    _faiss_available = False

```

This is where it fails, mind you `_faiss_available = importlib.util.find_spec("faiss")` line above works just fine, but fails on the `_faiss_version = importlib_metadata.version("faiss")` line unable to find the faiss package.

Not sure if it’s an issue in the repo or something wrong on my side. Any experience with this?

Thank you!

---

<div class="post-metadata">

**Author:** ![akinolawilson](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/akinolawilson/32/999_2.png) [@akinolawilson](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fakinolawilson)%5C
**Post date:** [February 8, 2021, 5:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-import-faiss%2F3439%2F2 "2021-02-08T17:27:47Z")

</div>

I am facing the same issue. No version of the faiss library is being recognized as present in the environment I am running (installed through conda). I did solve this problem before, but I remember it being a pain; I built the library from source but had to deal with many issues along the way.

Edit:  
I was just able to get it to be recognised.

Solution:

if you are able to import the library ( from terminal start python):  
python  
import faiss  
print(faiss)

> > /home/akinwilson/miniconda3/envs/envRag/lib/python3.7/site-packages/faiss/ **init**.py

take the path;  
/home/akinwilson/miniconda3/envs/envRag/lib/python3.7/site-packages/faiss

then in your bashrc file:  
nano ~/.bashrc  
add this line to the end of the file:  
export PYTHONPATH=$HOME/miniconda3/lib/python3.8/site-packages/faiss  
Note that I have changed the home directory to a variable.  
save it.

Then finally, set the bashrc file as source again to reload the paths;  
source ~/.bashrc

Restart any virtual environment and everything should work fine.

---

<div class="post-metadata">

**Author:** ![mkserge](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@mkserge](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmkserge)%5C
**Post date:** [February 17, 2021, 8:07pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-import-faiss%2F3439%2F3 "2021-02-17T20:07:58Z")

</div>

Thanks, just to let you know, this has been fixed on the master branch now.

```auto
_faiss_available = importlib.util.find_spec("faiss") is not None
try:
    _faiss_version = importlib_metadata.version("faiss")
    logger.debug(f"Successfully imported faiss version {_faiss_version}")
except importlib_metadata.PackageNotFoundError:
    try:
        _faiss_version = importlib_metadata.version("faiss-cpu")
        logger.debug(f"Successfully imported faiss version {_faiss_version}")
    except importlib_metadata.PackageNotFoundError:
        _faiss_available = False

```

---

<div class="post-metadata">

**Author:** ![NarenSEG](https://avatars.discourse-cdn.com/v4/letter/n/77aa72/32.png) [@NarenSEG](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FNarenSEG)%5C
**Post date:** [March 12, 2024, 8:16pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-import-faiss%2F3439%2F4 "2024-03-12T20:16:40Z")

</div>

This issue still persists (if you have faiss-gpu installed)  
I think it should be updated as:

```auto
# We need to check all three `faiss` and `faiss-cpu` and `faiss-gpu`
try:
    _faiss_version = importlib.metadata.version("faiss")
    logger.debug(f"Successfully imported faiss version {_faiss_version}")
except importlib.metadata.PackageNotFoundError:
    try:
        _faiss_version = importlib.metadata.version("faiss-cpu")
        logger.debug(f"Successfully imported faiss version {_faiss_version}")
    except importlib.metadata.PackageNotFoundError:
        try:
            _faiss_version = importlib.metadata.version("faiss-gpu")
            logger.debug(f"Successfully imported faiss version {_faiss_version}")
        except importlib.metadata.PackageNotFoundError:
            _faiss_available = False

```

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [March 13, 2024, 7:38am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Funable-to-import-faiss%2F3439%2F5 "2024-03-13T07:38:54Z")

</div>

Would be great to open a PR 🙂
