MacOS Ventura13.4下Python3.7.0构建pybind .whl包安装报错咨询
问题背景
在MacOS Ventura13.4(x86_64)平台,使用Python3.7.0(官网64位安装包)执行python setup.py bdist_wheel生成.whl包后,运行pip install dist/my_file.whl时出现错误:
*.whl is not a supported wheel on this platform
环境配置:
- Python3.7.0(必须保留)
- pip 23.1.2
- pybind 2.9.1(必须保留)
- setuptools 39.0.1
- wheel 0.40.0
经测试,Python3.8环境下无此问题。
Python3.7.0的核心问题
Python3.7.0发布于2018年,未适配MacOS Ventura这类后续推出的高版本系统:
- 其内置的
sysconfig模块返回的平台标识符仍为旧版的macosx_10_6_x86_64,导致bdist_wheel生成的whl包带有该旧平台标签。 - 新版pip(23.1.2)对平台标签的校验逻辑更严格,会匹配当前系统对应的
macosx_13_0_x86_64标签,因此拒绝识别带有旧标签的whl包。 - Python3.8及后续版本已更新了平台标签生成逻辑,能适配新MacOS版本,所以不会出现此问题。
修复方法
方法1:打包时指定兼容平台标签
执行bdist_wheel时手动指定兼容的平台标签,强制生成适配旧标签格式的whl包(MacOS向下兼容该标签):
python setup.py bdist_wheel --plat-name macosx_10_6_x86_64
生成的whl包会带有macosx_10_6_x86_64标签,pip 23.1.2会认可该标签与Ventura的兼容性。
方法2:修改已生成的whl包文件名
找到生成的whl文件(例如my_file-0.1.0-cp37-cp37m-macosx_10_6_x86_64.whl),将文件名中的平台标签部分扩展为多版本兼容格式:
修改为:my_file-0.1.0-cp37-cp37m-macosx_10_6_x86_64.macosx_11_0_x86_64.macosx_12_0_x86_64.macosx_13_0_x86_64.whl
之后再执行pip install即可。
方法3:安装时强制指定平台参数
无需修改whl包,安装时通过pip参数强制指定平台标签,跳过严格校验:
pip install dist/my_file.whl --platform macosx_10_6_x86_64 --python-version 37 --abi cp37m --force-reinstall
方法4:自定义setup.py的打包逻辑
在setup.py中添加自定义的wheel打包类,强制指定平台标签:
from setuptools import setup import sys # 针对Python3.7的MacOS环境自定义wheel标签 if sys.platform == 'darwin' and sys.version_info[:2] == (3, 7): from wheel.bdist_wheel import bdist_wheel class CustomBdistWheel(bdist_wheel): def get_tag(self): python_tag, abi_tag, plat_tag = super().get_tag() # 强制设置为兼容的平台标签 plat_tag = 'macosx_10_6_x86_64' return python_tag, abi_tag, plat_tag setup( # 你的项目原有配置(name、version、packages等) cmdclass={'bdist_wheel': CustomBdistWheel}, )
之后正常执行python setup.py bdist_wheel即可生成兼容的whl包。
内容的提问来源于stack exchange,提问作者mascai

