Ubuntu 22.04.1 LTS安装Django依赖时mysqlclient==2.0.3报错求助
解决Ubuntu下安装mysqlclient时
mysql_config not found的问题 问题场景
在Ubuntu 22.04.1 LTS系统上克隆Django项目,当前Python版本为3.8.10、Django版本为2.2,执行pip install -r requirements.txt安装依赖包时,安装mysqlclient==2.0.3出现错误,核心提示为mysql_config not found(MySQL数据库服务运行正常)。
错误信息如下:
Collecting mysqlclient==2.0.3 Using cached mysqlclient-2.0.3.tar.gz (88 kB) Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [16 lines of output] /bin/sh: 1: mysql_config: not found /bin/sh: 1: mariadb_config: not found /bin/sh: 1: mysql_config: not found Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "/tmp/pip-install-490urkf8/mysqlclient_91ecf52f39004862be75d78177e00bdb/setup.py", line 15, in <module> metadata, options = get_config() File "/tmp/pip-install-490urkf8/mysqlclient_91ecf52f39004862be75d78177e00bdb/setup_posix.py", line 70, in get_config libs = mysql_config("libs") File "/tmp/pip-install-490urkf8/mysqlclient_91ecf52f39004862be75d78177e00bdb/setup_posix.py", line 31, in mysql_config raise OSError("{} not found".format(_mysql_config_path)) OSError: mysql_config not found mysql_config --version mariadb_config --version mysql_config --libs [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
解决方案
mysql_config是MySQL/MariaDB开发包提供的工具,mysqlclient作为C扩展库,安装时需要依赖对应的开发文件和编译工具,执行以下步骤解决:
- 安装基础编译依赖
sudo apt-get update sudo apt-get install python3-dev build-essential
- 安装MySQL/MariaDB开发库
- 若使用官方MySQL版本:
sudo apt-get install libmysqlclient-dev
- 若使用系统默认的MariaDB:
sudo apt-get install libmariadb-dev
- 重新执行依赖安装
pip install -r requirements.txt
内容的提问来源于stack exchange,提问作者Karishma Upadhyay
相关产品推荐
相关产品推荐

