python3 在macOS 下 pip install mysql 报错:Exception: Can not find valid pkg-config name.
pip install mysql 报错:
具体报错内容:
~ pip install mysql
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mysql
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/9a/52/8d29c58f6ae448a72fbc612955bd31accb930ca479a7ba7197f4ae4edec2/mysql-0.0.3-py3-none-any.whl (1.2 kB)
Collecting mysqlclient (from mysql)
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
╰─> [27 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/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
File "/usr/local/lib/python3.11/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/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/setuptools/build_meta.py", line 327, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=[])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/setuptools/build_meta.py", line 297, in _get_build_requires
self.run_setup()
File "/usr/local/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/setuptools/build_meta.py", line 313, 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_CFLAGS
和MYSQLCLIENT_LDFLAGS
[环境变量]。以下是一些可能的解决步骤:
安装mysql
先,确保你的Mac上安装了MySQL。如果尚未安装,可以通过Homebrew安装:
brew install mysql
安装pkg-config
pkg-config是一个帮助安装软件库的工具,可以提供编译和链接时需要的选项。需要安装它:
brew install pkg-config
配置环境变量(brew安装版)
安装完MySQL和pkg-config后,你需要找到MySQL的头文件和库文件的位置,并设置相应的环境变量。
首先,尝试找到MySQL的安装路径。如果你使用Homebrew安装MySQL,它通常位于/usr/local/Cellar/mysql/下。路径中的版本号可能不同,所以请根据实际情况调整。
然后,设置MYSQLCLIENT_CFLAGS和MYSQLCLIENT_LDFLAGS环境变量。在终端中运行以下命令(可以直接复制使用 或 根据你的MySQL安装路径调整):
export MYSQLCLIENT_CFLAGS=$(mysql_config --cflags)
export MYSQLCLIENT_LDFLAGS=$(mysql_config --libs)
note: Mac环境变量文件操作
# 编辑环境变量配置文件
vim ~/.profile
vim ~/.zshrc
# 加载生效
source ~/.profile
source ~/.zshrc
参考
- home 安装(清华大学镜像):https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)