CentOS 7 + Python 3.11 安装 SQLAlchemy 报错 greenlet 编译失败(阿里云 SCL Vault 源修复方案)
本文只采用一种最简单、成功率最高的方案:
切换到阿里云 CentOS 7 SCL Vault 源,安装 devtoolset-10 升级 gcc。
适合人群:
- CentOS 7(7.6 / 7.9 / 云厂商定制版)
- Python 3.11
- pip 安装 SQLAlchemy / greenlet 失败
关键词:
Failed building wheel for greenlet、command '/usr/bin/g++' failed、CentOS 7 EOL
一、问题现象
在 CentOS 7 上使用 Python 3.11 执行:
pipinstallSQLAlchemy常见报错如下:
error: command '/usr/bin/g++' failed with exit code 1 ERROR: Failed building wheel for greenlet或者:
× Failed to build installable wheels for some pyproject.toml based projects ╰─> greenlet二、问题根因(必须先搞清楚)
1️⃣ SQLAlchemy 依赖 greenlet
- SQLAlchemy 1.4+ / 2.x
- 默认依赖
greenlet greenlet是C++ 扩展模块,需要本地编译
2️⃣ Python 3.11 对编译器要求提高
- greenlet 在 Python 3.11 中使用了更新的 C++ 特性
- 至少需要 gcc ≥ 9(推荐 gcc 10)
3️⃣ CentOS 7 默认 gcc 太旧
CentOS 7 自带:
gcc 4.8.5👉无法编译 greenlet(这是必然结果)
三、为什么 yum 装不到 devtoolset?
原因只有一个:
CentOS 7 已 EOL,官方 mirrorlist 仓库已下线
典型报错:
Could not retrieve mirrorlist http://mirrorlist.centos.org Cannot find a valid baseurl for repo: centos-sclo-rh因此,继续使用官方源一定失败。
四、唯一采用的解决方案:切换阿里云 SCL Vault 源
✔ 不改系统 gcc
✔ 成功率高
✔ 生产环境可用
下面是完整可执行步骤。
五、完整修复流程(一步一步执行)
1️⃣ 备份原 SCL repo
sudomkdir-p /etc/yum.repos.d/backupsudomv/etc/yum.repos.d/CentOS-SCLo-*.repo* /etc/yum.repos.d/backup/2️⃣ 配置阿里云 CentOS 7 SCL Vault 源
新建 repo 文件:
sudotee/etc/yum.repos.d/CentOS-SCLo.repo<<'EOF' [centos-sclo-rh] name=CentOS-7 - SCLo rh baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/rh/ enabled=1 gpgcheck=0 [centos-sclo-sclo] name=CentOS-7 - SCLo sclo baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/sclo/ enabled=1 gpgcheck=0 EOF3️⃣ 刷新 yum 缓存
sudoyum clean allsudoyum makecache4️⃣ 安装 devtoolset-10(gcc 10)
sudoyuminstall-y devtoolset-105️⃣ 启用新 gcc(关键步骤)
sclenabledevtoolset-10bash验证:
gcc --version g++ --version# gcc 10.x6️⃣ 安装 greenlet / SQLAlchemy
pipinstall-U pip setuptools wheel pipinstall--no-cache-dir greenlet pipinstall--no-cache-dir SQLAlchemy验证:
python -<<'EOF' import greenlet import sqlalchemy print('OK') EOF六、常见错误总结
- ❌ 继续使用 mirrorlist.centos.org
- ❌ 只升级 pip,不升级 gcc
- ❌ 忽略 g++ 版本
只要gcc ≥ 9,greenlet 一定能编译通过。
七、结论
当你在 CentOS 7 + Python 3.11 环境中遇到:
Failed building wheel for greenlet请直接记住这一条结论:
换阿里云 SCL Vault 源,安装 devtoolset-10,就够了。
如果本文帮你节省了时间,欢迎点赞 / 收藏 / 转发。