当前位置: 首页 > news >正文

在 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

解决方案

  1. 安装系统依赖

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

    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 客户端开发库,提供编译所需的头文件和库。
  2. 使用 pip 安装 mysqlclient

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

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

      python3 -m pip install mysqlclient
      
  3. 检查 MySQL 或 MariaDB 是否已安装(可选)

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

      sudo apt install -y mysql-server
      
    • 如果使用 MariaDB:

      sudo apt install -y mariadb-server
      
  4. 设置环境变量(如果仍然失败)

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

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

      pip install mysqlclient
      
  5. 使用预编译的二进制包(可选)

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

      pip install --only-binary :all: mysqlclient
      
  6. 确保 Python 环境干净

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

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

相关文章:

  • Gemini Thinks Faster
  • 10 . Docker 中的 DockerFile 解析(各种指令说明)
  • 安装mysql5.7报错 mysqld.exe-系统错误 由于找不到MSVCP120.dll,无法继续执行代码。重新安装程序可能 会解决此问题。 确定
  • /etc/sysctl.conf——系统的配置文件
  • #define宏与编译时定义的本质对决:从const常量到typedef的类型安全演进
  • 第二章:基础概念精讲 - 第一节 - Tailwind CSS 响应式设计系统
  • Okay, But Please Don’t Stop Talking
  • Flutter编译问题记录
  • [Windows] 微软常用运行库合集版 2025.02.12
  • 【黑马点评优化】1-使用JWT登录认证+redis实现自动续期
  • Flutter 中的生命周期
  • 理解 WebGPU 中的 GPUQueue:GPU 的命令队列
  • 针对Prompt优化的深入分析
  • 储能能量管理监测系统在储能物联网中的应用优势
  • 【java】List<String> fruits = new ArrayList<>(); 这一句是什么
  • 机器视觉3D缺陷检测痛点
  • P9584 「MXOI Round 1」城市
  • QT使用SQLCipher加密SQLite
  • 词袋模型和词嵌入模型区别和关联分析(词袋模型是否属于词嵌入模型)
  • 网络安全概论——数字证书与公钥基础设施PKI
  • 让中小学生体验不同职业,上海中高职院校提供超5万个体验名额
  • 4月22城新房价格上涨:上海一二手房价环比均上涨,核心城市土地热带动市场热度提升
  • 马上评|劳动课该如何找回“存在感”
  • 美俄亥俄州北部发生火车撞人事故,致2人死亡、至少1人失踪
  • 西域都护府博物馆今日在新疆轮台县开馆
  • 俄乌刚谈完美国便筹划与俄乌领导人通话,目的几何?