Python指定版本包安装失败:'Getting requirements to build wheel'报错咨询
指定版本Python包安装失败的原因及解决办法
问题重现
执行pip install -r requirements.txt安装pillow==10.3.0时出现以下报错:
Collecting pillow==10.3.0 (from -r requirements.txt (line 6)) Using cached pillow-10.3.0.tar.gz (46.6 MB) Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [21 lines of output] Traceback (most recent call last): File "D:\LMS\venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 389, in <module> main() ~~~~^^ File "D:\LMS\venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) ~~~~^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\LMS\venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 143, in get_requires_for_build_wheel return hook(config_settings) File "C:\Users\wo\AppData\Local\Temp\pip-build-env-bxsls9dh\overlay\Lib\site-packages\setuptools\build_meta.py", line 334, in get_requires_for_build_wheel return self._get_build_requires(config_settings, requirements=[]) ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\wo\AppData\Local\Temp\pip-build-env-bxsls9dh\overlay\Lib\site-packages\setuptools\build_meta.py", line 304, in _get_build_requires self.run_setup() ~~~~~~~~~~~~~~^^ File "C:\Users\wo\AppData\Local\Temp\pip-build-env-bxsls9dh\overlay\Lib\site-packages\setuptools\build_meta.py", line 320, in run_setup exec(code, locals()) ~~~~^^^^^^^^^^^^^^^^ File "<string>", line 33, in <module> File "<string>", line 27, in get_version KeyError: '__version__' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip.
手动安装该版本仍失败,但pip install pillow可成功安装最新的11.1.0版本。
原因分析
- 包版本与构建工具不兼容:旧版本包(如Pillow 10.3.0)的构建脚本逻辑和新版pip、setuptools不匹配。比如Pillow 10.3.0的版本获取代码依赖特定变量定义,而新版setuptools执行构建时的上下文发生变化,导致抛出
KeyError: '__version__'。 - 缺少预编译Wheel包:pip优先安装预编译的Wheel包(无需源码编译),如果某个版本没有适配当前系统(如Windows 64位、特定Python版本)的Wheel包,就会回退到源码编译,过程中容易出现依赖缺失、脚本错误等问题。
- 环境差异:生成
requirements.txt的环境(如Python版本、操作系统、构建工具版本)与安装环境不一致,旧版本包可能无法适配新环境的依赖或工具链。
解决办法
- 升级构建工具:先更新pip、setuptools和wheel到最新版本,修复兼容性问题:
升级后再尝试安装指定版本包,多数情况下能解决构建脚本的兼容性问题。pip install --upgrade pip setuptools wheel - 手动安装对应Wheel包:找到对应包版本、适配当前系统和Python版本的Wheel文件,直接下载安装。比如Pillow 10.3.0适配Windows 64位、Python 3.11的Wheel包,可执行以下命令(替换为对应文件名):
pip install pillow-10.3.0-cp311-none-win_amd64.whl - 放宽版本约束:如果业务逻辑不需要严格依赖旧版本,修改
requirements.txt中的版本规则:- 将
pillow==10.3.0改为pillow>=10.3.0(允许安装10.3.0及以上兼容版本) - 或直接写
pillow,让pip自动选择适配当前环境的最新稳定版
- 将
- 检查Python版本兼容性:确认要安装的包版本支持当前Python版本。若不兼容,要么降级Python版本,要么选择兼容的包版本。
- 安装源码编译依赖:如果必须从源码编译,先安装系统依赖:
- Windows:安装Visual C++ Build Tools,勾选“Desktop development with C++”组件
- Linux(Debian/Ubuntu):执行
sudo apt-get install libjpeg-dev zlib1g-dev - macOS:执行
brew install libjpeg zlib
内容的提问来源于stack exchange,提问作者Md Mahmud Hasan
相关产品推荐
相关产品推荐

