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


说明:经过在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 <module>
          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 "<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.

解决

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

1. 安装MySQL

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

brew install mysql

2. 安装pkg-config

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

brew install pkg-config

3.配置环境变量

将下方3.13.2使用的版本取决你的安装方式,这些export命令添加到您的~/.bash_profile~/.zshrc、或其他shell配置文件中,可以使这些变量的设置在新的终端会话中保持有效。修改配置文件后,您需要重新加载配置,或者重新开启一个终端会话。重新加载配置文件的命令取决于您使用的shell,比如如果是bash,可以使用:

source ~/.bash_profile

如果是zsh,则使用:

source ~/.zshrc
3.1 (二选一)配置环境变量(brew安装版)

安装完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)
3.2 (二选一)配置环境变量(手动安装版)

如果mysql是手动安装在/usr/local/mysql下的,可以直接使用这个路径来设置MYSQLCLIENT_CFLAGSMYSQLCLIENT_LDFLAGS环境变量。重要的是找到mysql_config这个工具的确切位置,它通常位于MySQL安装目录的bin子目录下。

mysql_config应该位于/usr/local/mysql/bin/mysql_config。可以使用这个命令来生成正确的标志:

export MYSQLCLIENT_CFLAGS=$(/usr/local/mysql/bin/mysql_config --cflags)
export MYSQLCLIENT_LDFLAGS=$(/usr/local/mysql/bin/mysql_config --libs)

确保在运行这些命令之前,/usr/local/mysql/bin/mysql_config确实存在且可执行。您可以通过以下命令检查:

/usr/local/mysql/bin/mysql_config --version

如果这个命令返回了mysql_config的版本信息,说明它是可执行的且路径正确。
完成这些步骤后,您应该能够在Mac上成功安装mysqlclient

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

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

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

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

  • 31
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python链接MySQL驱动包,安装之后可以使用Python语言链接MySQL进行数据库操作 # mysqlclient [![Build Status](https://secure.travis-ci.org/PyMySQL/mysqlclient-python.png)](http://travis-ci.org/PyMySQL/mysqlclient-python) This is a fork of [MySQLdb1](https://github.com/farcepest/MySQLdb1). This project adds Python 3 support and bug fixes. I hope this fork is merged back to MySQLdb1 like distribute was merged back to setuptools. ## Install ### Prerequisites You may need to install the Python and MySQL development headers and libraries like so: `sudo apt-get install python-dev libmysqlclient-dev` # Debian / Ubuntu `sudo yum install python-devel mysql-devel` # Red Hat / CentOS On Windows, there are binary wheel you can install without MySQLConnector/C or MSVC. #### Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command : `sudo apt-get install python3-dev` # debian / Ubuntu `sudo yum install python3-devel ` # Red Hat / CentOS `brew install mysql-connector-c` # macOS (Homebrew) ### Install from PyPI `pip install mysqlclient` NOTE: Wheels for Windows may be not released with source package. You should pin version in your `requirements.txt` to avoid trying to install newest source package. ### Install from source 1. Download source by `git clone` or [zipfile](https://github.com/PyMySQL/mysqlclient-python/archive/master.zip). 2. Customize `site.cfg` 3. `python setup.py install` ### Documentation Documentation is hosted on [Read The Docs](https://mysqlclient.readthedocs.io/)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值