Mac安装mysqlclient报错:Can not find valid pkg-config name

Mac安装mysqlclient报错:Can not find valid pkg-config name

    • 错误信息
    • 解决
      • 1. 安装MySQL
      • 2. 安装pkg-config
      • 3. 配置环境变量

说明:经过在baidu,google两个小时的搜索,找不到解决方案,或者找到的都是无法解决的垃圾方案,所以问了ChatGPT4.0 ,解决方案是它提供的!
如果解决了你的问题,欢迎点赞+评论+收藏❤️噢~,如果有其他解决方案,欢迎评论区或私信发我,我将进行验证后更新在文章中..

错误信息

Collecting mysqlclient
  Using cached mysqlclient-2.2.1.tar.gz (89 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 "/Users/wusiwei/Documents/PycharmProject/tactics/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in 
          main()
        File "/Users/wusiwei/Documents/PycharmProject/tactics/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/Users/wusiwei/Documents/PycharmProject/tactics/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/private/var/folders/x5/tctqgfhd405brdfffx76h2qr0000gn/T/pip-build-env-prp4xid4/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
        File "/private/var/folders/x5/tctqgfhd405brdfffx76h2qr0000gn/T/pip-build-env-prp4xid4/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "/private/var/folders/x5/tctqgfhd405brdfffx76h2qr0000gn/T/pip-build-env-prp4xid4/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "", line 155, in 
        File "", line 49, in get_config_posix
        File "", 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.

解决

根据错误日志,安装mysqlclient时遇到的问题似乎与MacOS上的依赖配置有关。
错误提示建议手动指定MYSQLCLIENT_CFLAGSMYSQLCLIENT_LDFLAGS环境变量。以下是一些可能的解决步骤:

1. 安装MySQL

首先,确保你的Mac上安装了MySQL。如果尚未安装,可以通过Homebrew安装:

brew install mysql

2. 安装pkg-config

pkg-config是一个帮助安装软件库的工具,可以提供编译和链接时需要的选项。需要安装它:

brew install pkg-config

3. 配置环境变量

安装完MySQL和pkg-config后,你需要找到MySQL的头文件和库文件的位置,并设置相应的环境变量。
首先,尝试找到MySQL的安装路径。如果你使用Homebrew安装MySQL,它通常位于/usr/local/Cellar/mysql/下。路径中的版本号可能不同,所以请根据实际情况调整。

然后,设置MYSQLCLIENT_CFLAGSMYSQLCLIENT_LDFLAGS环境变量。在终端中运行以下命令(可以直接复制使用根据你的MySQL安装路径调整):

export MYSQLCLIENT_CFLAGS=$(mysql_config --cflags)
export MYSQLCLIENT_LDFLAGS=$(mysql_config --libs)

提示:Mac环境变量文件操作

# 编辑环境变量配置文件
vim ~/.profile
vim ~/.zshrc

# 加载生效
source ~/.profile
source ~/.zshrc

得之淡然,失之坦然,让生命中的每一天都充满着阳光,洋溢着希望。做好自己,无愧于心!

你可能感兴趣的:(python,django)