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

Macos m系列芯片环境下python3安装mysqlclient系列问题

最近学习python3,在安装mysqlclient的时候遇到了一些问题,直接使用哦pip install mysqlclient 直接报错了,记录一下解决方案。

环境信息
设备:Macbook Pro m1
系统:macos Sequoia 15.3.2
最终成功的python版本:Python3.13.3
pip版本:25.0.1
最终系统环境下mysqlclient版本:2.2.7 -> 对应mysql-client: 8.0.42

直接执行命令安装: pip3 install mysqlclient,报错信息:

Collecting mysqlclientUsing cached mysqlclient-2.2.7.tar.gz (91 kB)Installing build dependencies ... doneGetting requirements to build wheel ... errorerror: subprocess-exited-with-error× Getting requirements to build wheel did not run successfully.│ exit code: 1╰─> [35 lines of output]/bin/sh: pkg-config: command not found/bin/sh: pkg-config: command not found/bin/sh: pkg-config: command not found/bin/sh: pkg-config: command not foundTrying pkg-config --exists mysqlclientCommand 'pkg-config --exists mysqlclient' returned non-zero exit status 127.Trying pkg-config --exists mariadbCommand 'pkg-config --exists mariadb' returned non-zero exit status 127.Trying pkg-config --exists libmariadbCommand 'pkg-config --exists libmariadb' returned non-zero exit status 127.Trying pkg-config --exists perconaserverclientCommand 'pkg-config --exists perconaserverclient' returned non-zero exit status 127.Traceback (most recent call last):File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 389, in <module>main()~~~~^^File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 373, in mainjson_out["return_val"] = hook(**hook_input["kwargs"])~~~~^^^^^^^^^^^^^^^^^^^^^^^^File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 143, in get_requires_for_build_wheelreturn hook(config_settings)File "/private/var/folders/pg/lcz_0brx7dl64hcxtkjtrn1c0000gp/T/pip-build-env-lqq7jo65/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 331, in get_requires_for_build_wheelreturn self._get_build_requires(config_settings, requirements=[])~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/private/var/folders/pg/lcz_0brx7dl64hcxtkjtrn1c0000gp/T/pip-build-env-lqq7jo65/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 301, in _get_build_requiresself.run_setup()~~~~~~~~~~~~~~^^File "/private/var/folders/pg/lcz_0brx7dl64hcxtkjtrn1c0000gp/T/pip-build-env-lqq7jo65/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 317, in run_setupexec(code, locals())~~~~^^^^^^^^^^^^^^^^File "<string>", line 156, in <module>File "<string>", line 49, in get_config_posixFile "<string>", line 28, in find_package_nameException: 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.

看错误信息是少了pkg-config
执行命令:brew install pkg-config,也报错!!!

fatal: not in a git directory
Warning: No remote 'origin' in /opt/homebrew/Library/Taps/homebrew/homebrew-services, skipping update!
Running `brew update --auto-update`...
Warning: You are using macOS 15.
We do not provide support for this pre-release version.
It is expected behaviour that some formulae will fail to build in this pre-release version.
It is expected behaviour that Homebrew will be buggy and slow.
Do not create any issues about this on Homebrew's GitHub repositories.
Do not create any issues even if you think this message is unrelated.
Any opened issues will be immediately closed without response.
Do not ask for help from MacHomebrew on Twitter.
You may ask for help in Homebrew's discussions but are unlikely to receive a response.
Try to figure out the problem yourself and submit a fix as a pull request.
We will review it but may or may not accept it.==> Fetching pkg-config
==> Downloading https://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar
######################################################################## 100.0%
==> ./configure --prefix=/opt/homebrew/Cellar/pkg-config/0.29.2_3 --disable-host
==> make
Last 15 lines from /Users/nankexiansheng/Library/Logs/Homebrew/pkg-config/02.make:
gatomic.c:464:10: error: incompatible integer to pointer conversion passing 'gsize' (aka 'unsigned long') to parameter of type 'gpointer' (aka 'void *') [-Wint-conversion]464 |   return g_atomic_pointer_xor ((volatile gpointer *) atomic, val);|          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./gatomic.h:191:45: note: expanded from macro 'g_atomic_pointer_xor'191 |     (gsize) __sync_fetch_and_xor ((atomic), (val));                          \|                                             ^~~~~
4 errors generated.
make[6]: *** [libglib_2_0_la-gatomic.lo] Error 1
make[6]: *** Waiting for unfinished jobs....
make[5]: *** [all-recursive] Error 1
make[4]: *** [all] Error 2
make[3]: *** [all-recursive] Error 1
make[2]: *** [all] Error 2
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2Do not report this issue to Homebrew/brew or Homebrew/core!Error: You are using macOS 15.
We do not provide support for this pre-release version.
It is expected behaviour that some formulae will fail to build in this pre-release version.
It is expected behaviour that Homebrew will be buggy and slow.
Do not create any issues about this on Homebrew's GitHub repositories.
Do not create any issues even if you think this message is unrelated.
Any opened issues will be immediately closed without response.
Do not ask for help from MacHomebrew on Twitter.
You may ask for help in Homebrew's discussions but are unlikely to receive a response.
Try to figure out the problem yourself and submit a fix as a pull request.
We will review it but may or may not accept it.

解决方法:
mysqlclient安装
可以参考mysqlclient文档
首选确保你的macos已经安装配置好了brew,以及配置了国内源。

# 安装pkg-config
brew install pkg-config
注意:如果这里报错,先执行下brew install mysql;再执行brew install pkg-config命令
# 安装 mysql-client
brew install mysql-client@8.0
# 添加环境变量 在 ~/.zshrc 中添加下面这行
export PKG_CONFIG_PATH="$(brew --prefix)/opt/mysql-client@8.0/lib/pkgconfig"保存后 source ~/.zshrc 刷新环境变量通过 printenv 可以查看确认当前环境变量

接下来可以正常安装mysqlclient,这里我安装的是mysqlclient 2.2.7版本

注意⚠️:2.0.0 以上版本需要python3.8及以上才能兼容

pip3 install mysqlclient

如果正常安装完成,没有报错,进入当前python运行环境测试
在这里插入图片描述
如果能正常引入MySQLdb而没有报错,那么基本上算上顺利完成了。可以尝试在虚拟环境安装完依赖启动项目试试

相关文章:

  • Java 深度与实战 · 每日一读 :高频面试真题解析 · ReentrantLock / CAS / AQS 篇
  • JVM常见的垃圾回收器
  • PyDantic入门介绍:Python数据验证与解析的利器
  • WPF之XAML基础
  • Java + Spring Boot + MyBatis获取以及持久化sql语句的方法
  • Redux 容器 | 原理解析
  • shell编程基础知识及脚本示例
  • 设计模式每日硬核训练 Day 16:责任链模式(Chain of Responsibility Pattern)完整讲解与实战应用
  • 分析型数据库入门指南:如何选择适合你的实时分析工具?
  • 哈希表基础
  • Ollama 在本地分析文件夹中的文件
  • 本安型交换机 + TSN:煤矿智能化的关键拼图
  • AI大模型从0到1记录学习 linux day21
  • 【论文阅读】-周总结-第5周
  • IDEA中使用Git
  • Vue2、Vue3区别之响应式原理
  • 深入理解 Java 单例模式:从基础到最佳实践
  • 【项目篇之垃圾回收】仿照RabbitMQ模拟实现消息队列
  • 查回来的数据除了 id,其他字段都是 null
  • 自然语言处理之机器翻译:注意力机制在低资源翻译中的突破与哲思
  • 李勇已任内蒙古乌兰察布市委副书记,曾在中央编办任职
  • 人民日报:应对外贸行业风险挑战,稳企业就是稳就业
  • “下山虎”张名扬一回合摘下“狮心”:你们再嘘一个给我听听
  • 马上评|起名“朱雀玄武敕令”?姓名权别滥用
  • 牛市早报|商务部:目前中美之间未进行任何经贸谈判
  • 特写|“三峡千古情”出圈,一场演出给宜昌留下更多游客