pythonpipswiglangchainfaiss

Error installing faiss-cpu - No module named 'swig'


I'm trying to install faiss-cpu via pip (pip install faiss-cpu) and get the following error:

  × Building wheel for faiss-cpu (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [12 lines of output]
      running bdist_wheel
      running build
      running build_py
      running build_ext
      building 'faiss._swigfaiss' extension
      swigging faiss/faiss/python/swigfaiss.i to faiss/faiss/python/swigfaiss_wrap.cpp
      swig -python -c++ -Doverride= -I/usr/local/include -Ifaiss -doxygen -module swigfaiss -o faiss/faiss/python/swigfaiss_wrap.cpp faiss/faiss/python/swigfaiss.i
      Traceback (most recent call last):
        File "/Users/me/langchain/venv/bin/swig", line 5, in <module>
          from swig import swig
      ModuleNotFoundError: No module named 'swig'
      error: command '/Users/me/langchain/venv/bin/swig' failed with exit code 1
      [end of output]

I've searched around and tried various solutions but nothing has worked.

I have the following setup:

Python 3.12.1
pip 23.3.2
SWIG Version 4.1.1
Python swig package (via pip): 4.1.1.post1

Solution

  • faiss-cpu is not available in python-3.12. According to their page on pypi it's available from python-3.7 to python-3.11. You need to downgrade your python in order to install faiss-cpu in your system. Now you can either remove python-3.12 and install python-3.10 on your system. Or use conda to create a virtual env with python-3.10 like following:

    conda create -n myenv python=3.10
    

    then to activate:

    conda activate myenv
    

    now install faiss-cpu:

    pip install faiss-cpu