在 debian 12 上安装 mysqlclient 报错

报错如下

复制代码
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mysqlclient
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/61/68/810093cb579daae426794bbd9d88aa830fae296e85172d18cb0f0e5dd4bc/mysqlclient-2.2.7.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 "/root/miniconda3/envs/shop/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 389, in <module>
          main()
        File "/root/miniconda3/envs/shop/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 373, in main
          json_out["return_val"] = hook(**hook_input["kwargs"])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/root/miniconda3/envs/shop/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 143, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-ugykwsgl/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-ugykwsgl/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 304, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-ugykwsgl/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 320, in run_setup
          exec(code, locals())
        File "<string>", line 156, 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.

报错原因

在 Debian 12 系统上安装 mysqlclient 报错的原因通常是系统缺少必要的依赖库,无法正确编译 mysqlclient

解决方案

安装系统依赖

mysqlclient 需要一些开发库来进行编译和运行,确保安装以下软件包:

bash 复制代码
sudo apt update
sudo apt install -y build-essential python3-dev default-libmysqlclient-dev
  • build-essential:用于提供 C/C++ 编译器和其他工具。
  • python3-dev:Python 的开发头文件。
  • default-libmysqlclient-dev:MySQL 客户端开发库,提供编译所需的头文件和库。

使用 pip 安装 mysqlclient

  • 确保你的 Python 环境正确,并尝试重新安装 mysqlclient

    bash 复制代码
    pip install mysqlclient
  • 如果你使用的是特定 Python 版本(例如系统中有多个 Python 版本),请确保你使用的是正确的 pip

    bash 复制代码
    python3 -m pip install mysqlclient

检查 MySQL 或 MariaDB 是否已安装(可选)

  • 如果你的项目需要连接到 MySQL 或 MariaDB,请确保数据库已安装并运行。如果尚未安装,可以使用以下命令安装 MySQL 服务器:

    bash 复制代码
    sudo apt install -y mysql-server
  • 如果使用 MariaDB:

    bash 复制代码
    sudo apt install -y mariadb-server

设置环境变量(如果仍然失败)

  • 如果依然报错提示 Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually,可以手动设置编译环境变量:

    bash 复制代码
    export MYSQLCLIENT_CFLAGS=$(mysql_config --cflags)
    export MYSQLCLIENT_LDFLAGS=$(mysql_config --libs)
  • 然后再次尝试安装:

    bash 复制代码
    pip install mysqlclient

使用预编译的二进制包(可选)

  • 如果你的环境不需要编译,从 PyPI 安装预编译的 mysqlclient 二进制包可以避免依赖问题:

    bash 复制代码
    pip install --only-binary :all: mysqlclient

确保 Python 环境干净

  • 如果你使用的是 conda 或虚拟环境,建议在干净的环境中重新尝试。例如:

    bash 复制代码
    conda create -n myenv python=3.12
    conda activate myenv
    pip install mysqlclient
  • 通过以上步骤,应该可以成功安装 mysqlclient。如果问题仍未解决,可以提供更多信息,我将进一步协助!

相关推荐
西阳未落几秒前
Linux(14)——库的制作与原理
linux
444A4E25 分钟前
深入Linux进程优先级:Nice值与O(1)调度器原理
linux·操作系统
Jooolin25 分钟前
【编程史】Git是如何诞生的?这可并非计划之中...
linux·git·ai编程
云边有个稻草人28 分钟前
【Linux系统】第八节—进程概念(上)—冯诺依曼体系结构+操作系统+进程及进程状态+僵尸进程—详解!
linux·进程·冯诺依曼体系结构·pcb·僵尸进程·进程的状态·task_ struct
xian0gang39 分钟前
rk3588 区分两个相同的usb相机
linux
这儿有一堆花1 小时前
安全访问家中 Linux 服务器的远程方案 —— 专为单用户场景设计
linux·服务器·安全
超级小忍1 小时前
如何配置 MySQL 允许远程连接
数据库·mysql·adb
RussellFans1 小时前
Linux 文本三剑客(grep, awk, sed)
linux·运维·服务器
吹牛不交税1 小时前
sqlsugar WhereIF条件的大于等于和等于查出来的坑
数据库·mysql