Runtime error when importing FAISS

Summary

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/faiss.py", 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/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
  File "/mount/src/work_apps/apps_comb.py", line 1489, in <module>
    page_names_to_funcs[demo_name]()
  File "/mount/src/work_apps/apps_comb.py", line 1430, in contract_analysis
    main()
 File "/mount/src/work_apps/apps_comb.py", line 1378, in main
  docsearch = FAISS.from_texts(chunks, embeddings)
File "/home/adminuser/venv/lib/python3.10/site-packages/langchain/vectorstores/faiss.py", line 603, in from_texts
 return cls.__from(
File "/home/adminuser/venv/lib/python3.10/site-packages/langchain/vectorstores/faiss.py", line 557, in __from
faiss = dependable_faiss_import()
File "/home/adminuser/venv/lib/python3.10/site-packages/langchain/vectorstores/faiss.py", 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:
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`) - 
3.10
- Using Conda? PipEnv? PyEnv? Pex?     
No
- OS version:
MacOS 13.4.1
- Browser version:
Safari, Chrome

### Requirements file

beautifulsoup4==4.11.2
docx==0.2.4
langchain==0.0.273
openai==0.27.6
pandas==2.0.2
pdfplumber==0.9.0
python_docx==0.8.11
Requests==2.31.0
streamlit==1.25.0
tiktoken
faiss-cpu

### Links

* Link to your GitHub repo:
* Link to your deployed app: https://workapps-rixy8gkhsbzbfxfpweylgh.streamlit.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 filename.py " instead of running just "streamlit run filename.py "
This solution worked for me.