You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将在线项目升级至Python 3.10时安装依赖遇报错求助

Python 3.10升级后Windows下mysqlclient安装报错解决方案

问题核心

升级到Python3.10后执行pip install -r requirements.txt时,mysqlclient安装失败,报错包括:

ERROR1: python setup.py bdist_wheel did not run successfully.
ERROR2: Running setup.py install for mysqlclient did not run successfully.
ERROR3: error: command '/opt/rh/gcc-toolset-9/root/bin/gcc' failed: No such file or directory
error: legacy-install-failure

本质是Windows环境下触发了mysqlclient的源码编译,但缺少适配的编译工具,或环境变量存在错误的Linux路径指向,同时Python版本升级后需要匹配对应版本的mysqlclient包。

解决步骤

  • 优先安装预编译wheel包(避免源码编译)

    1. 清理之前的失败安装残留:
      pip uninstall mysqlclient -y
      
    2. 升级pip和构建工具:
      python -m pip install --upgrade pip setuptools wheel
      
    3. 安装适配Python3.10的mysqlclient预编译版本(以2.2.0为例,该版本支持Python3.7-3.11):
      pip install mysqlclient==2.2.0
      
      如果直接安装失败,可手动下载对应Windows架构的wheel文件(例如mysqlclient-2.2.0-cp310-cp310-win_amd64.whl),然后本地安装:
      pip install D:\downloads\mysqlclient-2.2.0-cp310-cp310-win_amd64.whl
      
  • 若需源码编译(仅当预编译包无法使用时)

    1. 安装Visual Studio Build Tools,勾选「C++开发工具」组件,确保包含MSVC编译器和对应版本的Windows SDK。
    2. 检查系统环境变量,删除指向Linux gcc的错误路径(如/opt/rh/gcc-toolset-9/root/bin),确保MSVC编译器路径在PATH中。
    3. 安装MySQL Connector/C,下载对应Windows版本,安装时勾选「Add to PATH」选项,或手动将其bin目录添加到系统PATH。

验证安装

执行以下命令确认mysqlclient安装成功:

python -c "import MySQLdb; print(MySQLdb.__version__)"

内容的提问来源于stack exchange,提问作者David Henson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 16:30:59