Runtime error when importing FAISS


After this has been working before, all of a sudden, I receive an import error:

File "/home/adminuser/venv/lib/python3.10/site-packages/langchain/vectorstores/", line 47, in dependable_faiss_import
    import faiss
ModuleNotFoundError: No module named 'faiss'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/adminuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/", line 552, in _run_script
    exec(code, module.__dict__)
  File "/mount/src/work_apps/", line 1489, in <module>
  File "/mount/src/work_apps/", line 1430, in contract_analysis
 File "/mount/src/work_apps/", line 1378, in main
  docsearch = FAISS.from_texts(chunks, embeddings)
File "/home/adminuser/venv/lib/python3.10/site-packages/langchain/vectorstores/", line 603, in from_texts
 return cls.__from(
File "/home/adminuser/venv/lib/python3.10/site-packages/langchain/vectorstores/", line 557, in __from
faiss = dependable_faiss_import()
File "/home/adminuser/venv/lib/python3.10/site-packages/langchain/vectorstores/", line 49, in dependable_faiss_import
raise ImportError(
ImportError: Could not import faiss python package. Please install it with `pip install faiss-gpu` (for CUDA supported GPU) or `pip install faiss-cpu` (depending on Python version).

**Expected behavior:**

it worked before - 

import streamlit as st
import os
import sys
import re
import time
import openai
import subprocess
import platform
import pdfplumber
from docx import Document
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
import pandas as pd
import time
from bs4 import BeautifulSoup
import requests
from urllib.parse import urlparse, urljoin
import tempfile
#import extract_msg
from email.header import decode_header

- Streamlit version: (get it with `$ streamlit version`)       
Found Streamlit version 1.25.0 in the environment
- Python version: (get it with `$ python --version`) - 
- Using Conda? PipEnv? PyEnv? Pex?     
- OS version:
MacOS 13.4.1
- Browser version:
Safari, Chrome

### Requirements file


### Links

* Link to your GitHub repo:
* Link to your deployed app:

### Additional information

worked fine until yesterday (problems only arise when handling big docs that require chunking, embedding and faiss q&a chain)
works fine on local machine

If needed, add any other context about the problem here.

Try to forcefully reinstall the Faiss.

pip install faiss-cpu --force-reinstall

You can visit the link provided to get more idea on this problem:

it is really strange. On my local system, faiss is correctly installed - the same streamlit app runs without problems on localhost. So I am not sure what a forced re-install brings - unless I could do this on the remote machine (what I thought the requirements.txt is for). I tried virtually every version of faiss (and tiktoken, which was another culprit) re-arranged the orders in the requirements.txt and nothing helped. But then - very much to my surprise when I selected reboot-app (which I tried before on my home Mac) on my windows-driven work-machine, the app was “in the oven” for a very long time and it felt like it would be installing every python dependency since the early 90s… Well, long story short, that re-boot did it and it works again. But why? I have no idea. Does it matter to the remote system, what dependencies I have installed locally? Does it make a difference from what machine I am deploying or rebooting? Thanks to all who help me shed some light on this.

That’s just the expected behavior if you did everything right. Why faiss wasn’t installed before the reboot, I coudn’t tell. It might be a mistake on your part, an issue in Streamlit Cloud, pip acting up…

No and no. All that matters is what you have in your github repository, what settings you choose when deploying and whether you reboot the application (some changes require a reboot).

Thanks for clarifying - that’s also what I thought. Hopefully it stays more stable in the future…

Try running "python -m streamlit run " instead of running just "streamlit run "
This solution worked for me.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.