sballoon7
sballoon7

Reputation: 61

ModuleNotFoundError: No module named 'swig' but swig is already installed

I'm setting up my virtual environment and am trying to install faiss-cpu. The problem is that I keep getting this error:

\`ERROR: Command errored out with exit status 1:
command: /scratch1/skzhang/NNK_params/nnk_param_env/bin/python3 /scratch1/skzhang/NNK_params/nnk_param_env/lib64/python3.6/site-packages/pip/\_vendor/pep517/
in_process/\_in_process.py build_wheel /tmp/tmpffzso5pd
cwd: /tmp/pip-install-wd2v3dbd/faiss-cpu_83e2fb8a63d342bf9619c47cf685b0c7
Complete output (12 lines):
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 -DSWIGWORDSIZE64 -module swigfaiss -o faiss/faiss/python/swigfaiss_wrap.cpp faiss/faiss/p
ython/swigfaiss.i
Traceback (most recent call last):
File "/scratch1/skzhang/NNK_params/nnk_param_env/bin/swig", line 5, in \<module\>
from swig import swig
ModuleNotFoundError: No module named 'swig'
error: command 'swig' failed with exit status 1
-

ERROR: Failed building wheel for faiss-cpu
ERROR: Could not build wheels for faiss-cpu, which is required to install pyproject.toml-based projects\`

I ran pip3 install swig --upgrade already that that went successfully. I also tried uninstalling and reinstalling but still nothing. Does anyone know how I can fix this? Also, I'm using Python 3.6.8 if that's useful at all.

Upvotes: 4

Views: 6615

Answers (5)

Dmitry Zvenkov
Dmitry Zvenkov

Reputation: 111

Faced this issue on windows and solution was to install swig using choco: choco install swig

sourced from https://github.com/openai/spinningup/issues/32

Upvotes: 0

Max Haase
Max Haase

Reputation: 206

Make sure SWIG is correctly installed and accessible in your virtual environment. If it’s installed, add its path to your PATH variable:

export PATH=/path/to/swig:$PATH

Then, reinstall faiss-cpu.

Upvotes: 0

Mark Peschel
Mark Peschel

Reputation: 334

I ran into a similar issue trying to pip install gymnasium[box-2d] on Arch Linux. I tried running pip install swig and got a similar error to yours:

      swig -python -c++ -IBox2D -small -O -includeall -ignoremissing -w201 -globals b2Globals -outdir library/Box2D -keyword -w511 -D_SWIG_KWARGS -o Box2D/Box2D_wrap.cpp Box2D/Box2D.i
      Traceback (most recent call last):
        File "/home/mpeschel/projects/panic/minigrid/venv/bin/swig", line 5, in <module>
          from swig import swig
      ModuleNotFoundError: No module named 'swig'
      error: command '/home/mpeschel/projects/panic/minigrid/venv/bin/swig' failed with exit code 1
      [end of output]

My solution was to install the system swig package: pacman -S swig.

Upvotes: 2

Wang Weixuan
Wang Weixuan

Reputation: 350

The problem is due to undeclared build dependencies in the project you are trying to install. According to PEP-518, the file pyproject.toml should be added to that project with the following contents:

[build-system]
requires = ["setuptools", "swig"]
# and possibly other dependencies

During a build, pip sets the environment variable PYTHONNOUSERSITE, and uses the declared dependencies to create an isolated build environment. That explains why you still get ModuleNotFoundError after swig is installed.

Upvotes: 0

sballoon7
sballoon7

Reputation: 61

Ok update is that it's working now. I think there was something wrong with the virtual environment I set up because after I stopped using it faiss-cpu seems to install without a problem. Not exactly sure what I was doing wrong earlier though.

Upvotes: 2

Related Questions