安装PaddleClas时出现faiss-cpu编译错误,求解决方法
解决PaddleClas安装时faiss-cpu编译缺失头文件的问题
问题现象
安装PaddleClas过程中,安装swig与faiss-cpu后出现编译错误,swig.exe执行失败,无法找到多个faiss头文件,错误日志如下:
Building wheels for collected packages: faiss-cpu Building wheel for faiss-cpu (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [84 lines of output] C:\Users\anhdu\Documents\.vscode\PaddleOCR_test_env\lib\site-packages\setuptools\__init__.py:80: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated. !! ******************************************************************************** Requirements should be satisfied by a PEP 517 installer. If you are using pip, you can try `pip install --use-pep517`. ******************************************************************************** !! dist.fetch_build_eggs(dist.setup_requires) 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 -DSWIGWIN -o faiss\faiss\python\swigfaiss_wrap.cpp faiss\faiss\python\swigfaiss.i faiss\faiss\python\swigfaiss.i(250) : Error: Unable to find 'faiss\impl\platform_macros.h' faiss\faiss\python\swigfaiss.i(254) : Error: Unable to find 'faiss\utils\ordered_key_value.h' faiss\faiss\python\swigfaiss.i(255) : Error: Unable to find 'faiss\utils\Heap.h' faiss\faiss\python\swigfaiss.i(260) : Error: Unable to find 'faiss\utils\AlignedTable.h' faiss\faiss\python\swigfaiss.i(261) : Error: Unable to find 'faiss\utils\partitioning.h' faiss\faiss\python\swigfaiss.i(262) : Error: Unable to find 'faiss\utils\hamming.h' error: command 'C:\\Users\\anhdu\\Documents\\.vscode\\PaddleOCR_test_env\\Scripts\\swig.exe' failed 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 Running setup.py clean for faiss-cpu Failed to build faiss-cpu ERROR: Could not build wheels for faiss-cpu, which is required to install pyproject.toml-based projects
解决方法
1. 优先安装预编译版faiss-cpu(推荐)
Windows下源码编译faiss-cpu容易出现路径依赖问题,直接安装预编译包可跳过编译步骤:
pip install faiss-cpu==1.7.4 -i https://pypi.tuna.tsinghua.edu.cn/simple
1.7.4版本对Windows环境兼容性较好,适配Python 3.7-3.10版本。
2. 若需源码编译,修正头文件路径
如果必须从源码编译,需手动指定swig的头文件搜索路径:
- 克隆faiss源码仓库:
git clone https://github.com/facebookresearch/faiss.git - 进入faiss目录,执行带参数的安装命令:
cd faiss pip install . --install-option="--swig-opts=-I./faiss"
该命令强制让swig指向本地faiss源码中的头文件目录,解决路径识别错误。
3. 检查环境依赖版本
- Swig版本:确保使用4.0及以上版本,旧版本可能存在语法兼容问题。执行
swig -version查看版本,若版本过低,下载最新Windows版swig并添加到系统PATH。 - Cmake版本:要求≥3.18,低版本可能无法正确处理faiss的编译配置。
- Python版本:推荐使用3.7-3.10,高版本Python(如3.11+)可能未被faiss源码完全适配。
内容的提问来源于stack exchange,提问作者Anh Lê
相关产品推荐
相关产品推荐

