异常是关于mysqlclient和PyMySQL的
这个异常有些老生常谈了,但是至今都是一个问题,尤其是Python3在不同系统上的兼容性相差较大。这次记录的版本是Python3.10(Python3.11),在Windows11系统版本上可以直接安装mysqlclient
我印象已经有很长一段时间,可以在Django中使用PyMySQL替代mysqlclient,并且这些年mysqlclient有各种依赖问题导致安装失败,相反PyMySQL则可以直接安装。
但是目前发现最新的Django5+Python3.11版本在Windows上正常安装mysqlclient了,且使用PyMySQL则导致一些不兼容的安装问题。而ubuntu22.04容器中的同版本Python就出现了下面的报错:
Collecting mysqlclient==2.2.4 Using cached https://pypi.tuna.tsinghua.edu.cn/packages/79/33/996dc0ba3f03e2399adc91a7de1f61cb14b57ebdb4cc6eca8a78723043cb/mysqlclient-2.2.4.tar.gz (90 kB) 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 ╰─> [27 lines of output] /bin/sh: 1: pkg-config: not found /bin/sh: 1: pkg-config: not found /bin/sh: 1: pkg-config: not found Trying pkg-config --exists mysqlclient Command 'pkg-config --exists mysqlclient' returned non-zero exit status 127. Trying pkg-config --exists mariadb Command 'pkg-config --exists mariadb' returned non-zero exit status 127. Trying pkg-config --exists libmariadb Command 'pkg-config --exists libmariadb' returned non-zero exit status 127. Traceback (most recent call last): File "/home/env/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module> main() File "/home/env/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main json_out['return_val'] = hook(**hook_input['kwargs']) File "/home/env/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 130, in get_requires_for_build_wheel return hook(config_settings) File "/tmp/pip-build-env-wrm_4di5/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel return self._get_build_requires(config_settings, requirements=['wheel']) File "/tmp/pip-build-env-wrm_4di5/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires self.run_setup() File "/tmp/pip-build-env-wrm_4di5/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 311, in run_setup exec(code, locals()) File "<string>", line 155, in <module> File "<string>", line 49, in get_config_posix File "<string>", line 28, in find_package_name Exception: Can not find valid pkg-config name. Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually [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.
依据异常提示,这个错误提示是由于缺少 pkg-config 工具导致的。pkg-config 是一个帮助定位编译器和链接器标志的工具,某些 Python 包需要它来定位依赖库的位置。
这里推荐先后安装以下两个包:
apt install pkg-config
另外,安装mysql-client时,libmysqlclient-dev 是 MySQL 客户端库的开发包,通常用于构建需要连接到 MySQL 的软件。在大多数情况下,需要构建 Python 的 MySQL 客户端库(如 mysqlclient),您需要安装 libmysqlclient-dev。
apt install libmysqlclient-dev