Failed to build faiss-cpu :: ModuleNotFoundError: No module named 'swig'

Hello Friends,

I am trying to install faiss-cpu on my Windows | Streamlit where getting following error and looking for any idea how to resolve this. Appreciate your kind support.

Building wheel for faiss-cpu (pyproject.toml) … error
error: subprocess-exited-with-error

× Building wheel for faiss-cpu (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [13 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.exe -python -c++ -Doverride= -I/usr/local/include -Ifaiss -doxygen -DSWIGWIN -module swigfaiss -o faiss\faiss\python\swigfaiss_wrap.cpp faiss\faiss\python\swigfaiss.i
Traceback (most recent call last):
File “”, line 198, in _run_module_as_main
File “”, line 88, in run_code
File "E:\AI-ML-Code\GenerativeAI\GenAI_LearnLangChainLLMAppsOpenAI_Llama_2\Sec09_PDF_ChatApp_StreamlitApp\PDF_ChatApp_StreamlitApp\venv\Scripts\swig.exe_main
.py", line 4
, in
ModuleNotFoundError: No module named ‘swig’
error: command ‘E:\AI-ML-Code\GenerativeAI\GenAI_LearnLangChainLLMAppsOpenAI_Llama_2\Sec09_PDF_ChatApp_StreamlitApp\PDF_ChatApp_StreamlitApp\venv/Scripts\swig.exe’ fail
ed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for faiss-cpu
Successfully built tiktoken
Failed to build faiss-cpu
ERROR: Could not build wheels for faiss-cpu, which is required to install pyproject.toml-based projects

Hi @BikashKumar

Therer seems to be a missing dependency that is required for building the wheel for the package:

As you’re using a Windows, I would recommend getting swig installed first and then try again with building faiss-cpu.

More info here:

Hope this helps!

Thank you but still struggling with this,

I would appreciate your help on this.

Use python 3.11 until faiss-cpu provides binary wheels for python 3.12.


Hi @BikashKumar

It seems to be an issue with Swig installation. Please see this related post:

Thank you dataprofessor, Thank you Goyo. The issue is resolved.

Can you please confirm which suggestion solved you issue? I am facing similar challenge. I already have SWIG version 4.1.1; however I am using Python 3.12.1.
Just wondering, if I need to downgrade my python to 3.11 or below?

Hello @ashishsrivastava - pls check once if the software / package you are installing has a compatibility with Python 3.12.1, else you need to create a project based on the Python compatibility version which you may see in the definition page of the package. Eg. in case of FAISS db, on the left hand side it has clearly mentioned which is the top most compatible version of supported Python that you need to have in the project.

Hope this clarifies the concern.

Thanks @BikashKumar. Somehow I missed checking the dependencies earlier.
I checked again and found that faiss-cpu is supported till Python 3.11 only till now.
I switched back to Python 3.11 and it worked now.
Thanks for pointing out!

1 Like