Debian12下安装Python-MIP时遭遇CFFI编译失败问题求助
解决Debian 12上安装mip包时cffi编译失败的问题
问题现象
执行pip install mip时,cffi==1.15.0编译报错,提示找不到Python.h文件导致gcc编译失败,完整错误日志如下:
pip install mip Collecting mip Using cached mip-1.14.2-py3-none-any.whl.metadata (21 kB) Collecting cffi==1.15.0 (from mip) Using cached cffi-1.15.0.tar.gz (484 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Requirement already satisfied: pycparser in ./.venv/lib/python3.12/site-packages (from cffi==1.15.0->mip) (2.22) Using cached mip-1.14.2-py3-none-any.whl (15.3 MB) Building wheels for collected packages: cffi Building wheel for cffi (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for cffi (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [33 lines of output] running bdist_wheel running build running build_py creating build/lib.linux-x86_64-cpython-312/cffi copying cffi/__init__.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/api.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/commontypes.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/cparser.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/error.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/ffiplatform.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/lock.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/model.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/pkgconfig.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/recompiler.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/vengine_gen.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/verifier.py -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/_cffi_include.h -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/parse_c_type.h -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/_embedding.h -> build/lib.linux-x86_64-cpython-312/cffi copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-cpython-312/cffi running build_ext building '_cffi_backend' extension creating build/temp.linux-x86_64-cpython-312/c x86_64-linux-gnu-gcc -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall -fPIC -DUSE__THREAD -DHAVE_SYNC_SYNCHRONIZE -I/home/santos/Documentos/mathprog/.venv/include -I/usr/include/python3.12 -c c/_cffi_backend.c -o build/temp.linux-x86_64-cpython-312/c/_cffi_backend.o c/_cffi_backend.c:2:10: fatal error: Python.h: Arquivo ou diretório inexistente 2 | #include <Python.h> | ^~~~~~~~~~ compilation terminated. error: command '/usr/bin/x86_64-linux-gnu-gcc' 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 cffi Failed to build cffi ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (cffi)
解决步骤
1. 安装Python开发依赖包
Python.h是Python开发包的核心文件,从日志可知你使用的是Python 3.12,执行以下命令安装对应版本的开发包:
sudo apt-get update sudo apt-get install python3.12-dev
若不确定当前Python版本,可安装通用的Python3开发包:
sudo apt-get install python3-dev
2. 补全编译工具链
编译cffi需要gcc等基础编译工具,安装build-essential包补充完整工具链:
sudo apt-get install build-essential
3. 重新安装mip包
依赖安装完成后,再次执行安装命令:
pip install mip
备选方案:升级cffi版本(若上述步骤无效)
mip指定的cffi==1.15.0版本较旧,可能与Python 3.12兼容性不佳。可尝试先安装最新兼容版cffi,再单独安装mip:
pip install cffi --upgrade pip install mip --no-deps
内容的提问来源于stack exchange,提问作者Bruno Santos
相关产品推荐
相关产品推荐

