Loading

CentOS 7.9 python 虚拟环境安装mysqlclient失败

环境

  • python3.10.13
  • CentOS 7.9

懵懂安装

pip install mysqlclient

报错

(GrowthVision) [root@localhost Backend]# pip install mysqlclient
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mysqlclient
  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
  ╰─> [24 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.
      Traceback (most recent call last):
        File "/usr/local/var/venv/GrowthVision/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/usr/local/var/venv/GrowthVision/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/usr/local/var/venv/GrowthVision/lib/python3.10/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-2b07mcxc/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-2b07mcxc/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-2b07mcxc/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.
(GrowthVision) [root@localhost Backend]# 

原因

这个问题是由于缺少 pkg-config 名称导致的。手动设置 MYSQLCLIENT_CFLAGS 和 MYSQLCLIENT_LDFLAGS 环境变量,以便告诉安装程序正确的编译标志。

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.
Traceback (most recent call last):
File "/usr/local/var/venv/GrowthVision/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()

解决

  1. 找到 mysql_config 文件的路径
(GrowthVision) [root@localhost Backend]# which mysql_config
/usr/local/mysql8/bin/mysql_config
  1. 设置 MYSQLCLIENT_CFLAGS 和 MYSQLCLIENT_LDFLAGS 环境变量,将 mysql_config 的路径传递给它们
(GrowthVision) [root@localhost Backend]# export MYSQLCLIENT_CFLAGS=$(mysql_config --cflags)
(GrowthVision) [root@localhost Backend]# export MYSQLCLIENT_LDFLAGS=$(mysql_config --libs)
  1. 再次安装mysqlclient
(GrowthVision) [root@localhost Backend]# pip install mysqlclient
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mysqlclient
  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 ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (pyproject.toml) ... done
  Created wheel for mysqlclient: filename=mysqlclient-2.2.4-cp310-cp310-linux_x86_64.whl size=120911 sha256=a9ceea8f41114389914bfa3010f9658b9e2007093951d4ebef5e3c9d38714373
  Stored in directory: /root/.cache/pip/wheels/f9/fa/0a/9f7144a7a3d51e80ecf47c24e9d44baa083dcbe65ef2776b39
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.2.4

完结撒花🎉

posted @ 2024-04-16 14:52  ThankCAT  阅读(604)  评论(0编辑  收藏  举报