You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装faiss-cpu报错:找不到'swig'模块

安装faiss-cpu时触发ModuleNotFoundError: No module named 'swig'的解决办法

执行pip install faiss-cpu命令时出现以下报错:

× 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]

当前环境配置:

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

解决步骤

  • 卸载通过pip安装的swig包,它会和系统原生SWIG工具冲突:
    pip uninstall -y swig
    
  • 验证系统原生SWIG的路径,确保它在环境变量PATH中优先级更高:
    which swig
    
    正常应返回类似/usr/local/bin/swig的路径(对应已安装的SWIG 4.1.1)
  • 重新执行faiss-cpu安装命令:
    pip install faiss-cpu
    
  • 若仍存在编译问题,直接安装预编译的wheel包跳过源码编译:
    pip install faiss-cpu --only-binary :all:
    

内容的提问来源于stack exchange,提问作者sufh243

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 03:03:35