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

Python 3.9环境下无法安装mysqlclient的问题求助

解决WSL Ubuntu 20.04下Python3.9安装mysqlclient时的Python.h缺失错误

问题重现

执行命令:

pip3 install mysqlclient

触发如下错误:

x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/usr/include/mysql -I/home/eweinsh/microservice/src/auth/_venv/include -I/usr/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-cpython-39/MySQLdb/_mysql.o -std=c99
MySQLdb/_mysql.c:46:10: fatal error: Python.h: No such file or directory
46 | #include "Python.h"
| ^~~~~~~~~~
compilation terminated.
error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

系统环境:Python 3.9,运行于WSL中的Ubuntu 20.04。已尝试重装带推荐包的Python3、添加MySQL配置文件,问题仍未解决。

解决方案

错误核心是缺少编译Python扩展所需的Python.h头文件,同时mysqlclient编译依赖MySQL开发库,按以下步骤操作:

  1. 安装Python3.9的开发依赖包:
sudo apt-get update && sudo apt-get install python3.9-dev
  1. 安装MySQL开发依赖库:
sudo apt-get install default-libmysqlclient-dev
  1. 重新执行mysqlclient安装命令:
pip3 install mysqlclient

说明

  • python3.9-dev包包含Python.h等编译Python扩展必需的头文件,Ubuntu默认安装Python时不会自动附带该包。
  • default-libmysqlclient-dev提供mysqlclient编译所需的MySQL头文件与库文件,确保编译流程能找到相关资源。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 12:20:27