使用Homebrew在MacOS安装tbb时遭遇Python.h缺失错误
解决Homebrew安装tbb时Python.h缺失的编译错误(MacOS 11.7.10)
问题场景
在MacOS 11.7.10系统下,通过Homebrew安装tbb(为后续安装OpenVINO做准备)时,编译阶段触发Python.h文件找不到的错误,核心报错片段如下:
==> cmake --build build/shared Last 15 lines from /Users/inspectorgloomy/Library/Logs/Homebrew/tbb/02.cmake: /Library/Developer/CommandLineTools/usr/bin/make -f python/CMakeFiles/python_build.dir/build.make python/CMakeFiles/python_build.dir/build [100%] Build and install to work directory the oneTBB Python module cd /tmp/tbb-20240201-34715-juqzow/oneTBB-2021.11.0/build/shared/python && /usr/local/bin/python3 python_build/setup.py build -bpython_build build_ext -I/tmp/tbb-20240201-34715-juqzow/oneTBB-2021.11.0/src/tbb/../../include -L/tmp/tbb-20240201-34715-juqzow/oneTBB-2021.11.0/build/shared/appleclang_13.0_cxx11_64_release install --prefix build --install-lib python_build/build/lib/python3.12/site-packages -f tbb/api_wrap.cpp:199:11: fatal error: 'Python.h' file not found # include <Python.h> ^~~~~~~~~~ 1 error generated. error: command '/usr/local/Homebrew/Library/Homebrew/shims/mac/super/clang' failed with exit code 1 make[2]: *** [python/CMakeFiles/python_build] Error 1 make[1]: *** [python/CMakeFiles/python_build.dir/all] Error 2 make: *** [all] Error 2
已确认系统中Python已安装,且其他Homebrew软件包可正常构建,仅tbb安装失败。
解决方案
1. 安装/重装Python开发包
Homebrew默认安装的Python可能未包含完整的开发组件(包括Python.h),针对报错中的Python 3.12版本,执行:
# 若未安装Python 3.12 brew install python@3.12 # 若已安装,重装确保开发文件齐全 brew reinstall python@3.12
2. 手动指定Python头文件路径给CMake
通过环境变量将Python头文件路径传递给编译工具,执行以下命令后再安装tbb:
export CFLAGS="-I$(python3 -c 'import sysconfig; print(sysconfig.get_path("include"))')" export CXXFLAGS="$CFLAGS" brew install tbb
3. 跳过Python模块编译(仅当不需要tbb Python绑定时)
如果仅需tbb的C++库用于OpenVINO,可禁用Python模块编译:
brew install tbb --without-python
4. 修复Command Line Tools
MacOS 11的Command Line Tools可能缺失部分头文件,重新安装确保完整性:
xcode-select --install
原因说明
报错中使用的Python 3.12,其头文件默认位于$(python3 -c 'import sysconfig; print(sysconfig.get_path("include"))')路径下,CMake在编译tbb的Python绑定阶段未自动识别该路径;此外MacOS 11作为旧系统,对高版本Python的兼容性需要额外配置开发组件路径。
内容的提问来源于stack exchange,提问作者user23330269
相关产品推荐
相关产品推荐

