faiss: Faiss import error when run in virtualenv by using own built Faiss-python

Summary

I have built faiss-core and faiss-python by myself. I installed python into my local virtual env and try to import faiss and I got an error, checked egg file, it does have _swigfaiss.so inside. I checked conda swigfaiss.py, it’s still using old swig_import_helper, not sure if caused by this you remove it by using swig create python/swigfaiss.py as follows:

https://github.com/facebookresearch/faiss/commit/7f5b22b0fff0882ce4afd93ce54cc2833a224909#diff-8cf6167d58ce775a08acafcfe6f40966

$ ls faiss-1.5.2-py3.6/faiss
__init__.py	__pycache__	_swigfaiss.so	swigfaiss.py

Platform

OS: centos 7

Faiss version: 1.5.2

Faiss compilation options:

 ./configure  --prefix=/usr --without-cuda --with-blas=/usr/lib64/libblas.so.3 --with-lapack=/usr/lib64/liblapack.so.3
make
sudo make install
make py
cd ~ && rm -rf env && python3 -m venv env
source env/bin/activate
cd ~/faiss && sudo make -C python install

Running on:

  • CPU
  • GPU

Interface:

  • C++
  • Python

Reproduction instructions

$ python
Python 3.6.7 | packaged by conda-forge | (default, Feb 28 2019, 09:07:38)  [GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import faiss
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/midas/env/lib/python3.6/site-packages/faiss-1.5.2-py3.6.egg/faiss/__init__.py", line 18, in <module>
  File "/home/midas/env/lib/python3.6/site-packages/faiss-1.5.2-py3.6.egg/faiss/swigfaiss.py", line 13, in <module>
ImportError: cannot import name '_swigfaiss'

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 23 (5 by maintainers)

Most upvoted comments

Just install three things in the following order and your problem Is solved

pip3 install faiss sudo apt-get install libopenblas-dev sudo apt-get install libomp-dev

Solution:

unzip the .egg file

create the folder “faiss-1.6.1-py3.5.egg” or whatever your error while import python3 comes up. mv faiss faiss-1.6.1-py3.5.egg

then check with import faiss

@mdouze This is definately a bug the egg file is not extracting properly.

I guess this might be related to egg file, when I do

$  PYTHONPATH=/home/midas/env/lib/python3.6/site-packages/faiss-1.5.2-py3.6.egg/faiss/ python -c "import _swigfaiss"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named '_swigfaiss'

after I unzip the egg file, I don’t see error as below

$  PYTHONPATH=/home/midas/env/lib/python3.6/site-packages/faiss-1.5.2-py3.6/faiss/ python -c "import _swigfaiss"

I guess this might be related to egg file, when I do

$  PYTHONPATH=/home/midas/env/lib/python3.6/site-packages/faiss-1.5.2-py3.6.egg/faiss/ python -c "import _swigfaiss"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named '_swigfaiss'

after I unzip the egg file, I don’t see error as below

$  PYTHONPATH=/home/midas/env/lib/python3.6/site-packages/faiss-1.5.2-py3.6/faiss/ python -c "import _swigfaiss"

where can I get the egg file? Could you share me the link.

The unzip strategy from @XinliYu and @deimsdeutsch work great. An alternative to changing the permissions of the egg file is to use python-pip instead:

cd <path_to_faiss>
cd ./build/faiss/python
python -m pip install .

rather than using the current install directions from step 3 (see below),

python setup.py install

Found the solution for me! Just unzip the egg file! unzip faiss-1.5.3-py3.6.egg

And then a new folder called faiss will be created in the site-package folder. Then import is successful!