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而没有报错,那么基本上算上顺利完成了。可以尝试在虚拟环境安装完依赖启动项目试试