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

Linux Mint 22下pip安装mysqlclient失败,报错'subprocess-exited-with-error'求助

Linux Mint 22下pip安装mysqlclient失败,报错'subprocess-exited-with-error'求助

各位好,我现在用的是Linux Mint 22,尝试用pip安装mysqlclient的时候一直失败,报subprocess-exited-with-error错误,折腾了好几种方法都没解决,来求大家帮忙看看。

直接运行pip install mysqlclient的完整错误日志如下:

(env) sh-5.2$ pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.2.6.tar.gz (91 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
  ╰─> [29 lines of output]
      Trying pkg-config --exists mysqlclient
      Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
      Trying pkg-config --exists mariadb
      Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
      Trying pkg-config --exists libmariadb
      Command 'pkg-config --exists libmariadb' returned non-zero exit status 1.
      Trying pkg-config --exists perconaserverclient
      Command 'pkg-config --exists perconaserverclient' returned non-zero exit status 1.
      Traceback (most recent call last):
        File "/home/usr/School/24-25/P2/project en samenwerken/Project/env/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/home/usr/School/24-25/P2/project en samenwerken/Project/env/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/usr/School/24-25/P2/project en samenwerken/Project/env/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-2g4_q5k5/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 334, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=[])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-2g4_q5k5/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 304, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-2g4_q5k5/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 320, 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.
(env) sh-5.2$ 

我已经尝试过的解决方法

  1. 安装所有依赖包
    看了其他帖子的建议,先装了所有需要的依赖包,用的命令是:
usr@usr-laptop:~$ sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config mysql-server libffi-dev libssl-dev libmysqlclient-dev

系统返回所有包都已经安装好了:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3-dev is already the newest version (3.12.3-0ubuntu2).
default-libmysqlclient-dev is already the newest version (1.1.0build1).
build-essential is already the newest version (12.10ubuntu1).
pkg-config is already the newest version (1.8.1-2build1).
mysql-server is already the newest version (8.0.40-0ubuntu0.24.04.1).
libffi-dev is already the newest version (3.4.6-1build1).
libssl-dev is already the newest version (3.0.13-0ubuntu3.4).
libmysqlclient-dev is already the newest version (8.0.40-0ubuntu0.24.04.1).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.

但装完之后再试安装mysqlclient还是同样的错误,而且pip本身没问题,其他包都能正常装。

  1. 参考同类问题的解决方案
    我也试过某个类似问题里的方案,安装pkg-config python3-dev default-libmysqlclient-dev build-essential,但这些包我本来就有了,所以还是没解决。

  2. 手动指定环境变量
    后来我先找了mysql头文件和库文件的位置:

usr@usr-laptop:~$ sudo find /usr/include -name "mysql.h"
/usr/include/mysql/mysql.h
usr@usr-laptop:~$ sudo find /usr/lib -name "libmysqlclient.so*"
/usr/lib/x86_64-linux-gnu/libmysqlclient.so
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.21.2.40
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.21

然后手动设置MYSQLCLIENT_CFLAGS和MYSQLCLIENT_LDFLAGS来安装:

(env) sh-5.2$ MYSQLCLIENT_CFLAGS="-I/usr/include/mysql" MYSQLCLIENT_LDFLAGS="-L/usr/lib/x86_64-linux-gnu -lmysqlclient" pip3 install mysqlclient

结果还是失败了,这次的错误是找不到mysql.h:

Collecting mysqlclient
  Using cached mysqlclient-2.2.6.tar.gz (91 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for mysqlclient (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [40 lines of output]
      # Options for building extension module:
        extra_compile_args: ['-I/usr/include/mysql', '-std=c99']
        extra_link_args: ['-L/usr/lib/x86_64-linux-gnu', '-lmysqlclient']
        define_macros: [('version_info', (2, 2, 6, 'final', 0)), ('__version__', '2.2.6')]
      running bdist_wheel
      running build
      running build_py
      creating build/lib.linux-x86_64-cpython-312/MySQLdb
      copying src/MySQLdb/__init__.py -> build/lib.linux-x86_64-cpython-312/MySQLdb
      copying src/MySQLdb/_exceptions.py -> build/lib.linux-x86_64-cpython-312/MySQLdb
      copying src/MySQLdb/connections.py -> build/lib.linux-x86_64-cpython-312/MySQLdb
      copying src/MySQLdb/converters.py -> build/lib.linux-x86_64-cpython-312/MySQLdb
      copying src/MySQLdb/cursors.py -> build/lib.linux-x86_64-cpython-312/MySQLdb
      copying src/MySQLdb/release.py -> build/lib.linux-x86_64-cpython-312/MySQLdb
      copying src/MySQLdb/times.py -> build/lib.linux-x86_64-cpython-312/MySQLdb
      creating build/lib.linux-x86_64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/CR.py -> build/lib.linux-x86_64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/ER.py -> build/lib.linux-x86_64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-cpython-312/MySQLdb/constants
      copying src/MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-cpython-312/MySQLdb/constants
      running egg_info
      writing src/mysqlclient.egg-info/PKG-INFO
      writing dependency_links to src/mysqlclient.egg-info/dependency_links.txt
      writing top-level names to src/mysqlclient.egg-info/top_level.txt
      reading manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
      reading manifest template 'MANIFEST.in'
      adding license file 'LICENSE'
      writing manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
      copying src/MySQLdb/_mysql.c -> build/lib.linux-x86_64-cpython-312/MySQLdb
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.linux-x86_64-cpython-312/src/MySQLdb
      x86_64-unknown-linux-gnu-gcc -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O3 -Wall -O2 -pipe -g -Wp,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -O3 -O2 -pipe -g -Wp,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -O3 -fPIC "-Dversion_info=(2, 2, 6, 'final', 0)" -D__version__=2.2.6 "-I/home/usr/School/24-25/P2/project en samenwerken/Project/env/include" -I/usr/include/python3.12 -I/usr/include/x86_64-linux-gnu/python3.12 -c src/MySQLdb/_mysql.c -o build/temp.linux-x86_64-cpython-312/src/MySQLdb/_mysql.o -I/usr/include/mysql -std=c99
      src/MySQLdb/_mysql.c:29:10: fatal error: mysql.h: No such file or directory
         29 | #include "mysql.h"
            |          ^~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-unknown-linux-gnu-gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
Failed to build mysqlclient
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (mysqlclient)
(env) sh-5.2$ 

现在实在没辙了,有没有大佬遇到过类似的情况?或者能帮我分析下为什么明明头文件路径是对的,手动指定了还是找不到呢?

备注:内容来源于stack exchange,提问作者piphelp

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 18:10:26