You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS M2安装Python mysqlclient库失败问题求助

MacOS M2 安装mysqlclient 失败:链接器找不到zlib库

在MacOS M2设备上,按照mysqlclient官方仓库的Homebrew安装指南(原针对Python 3 venv),尝试使用Poetry或无虚拟环境安装mysqlclient库均失败,核心报错为链接器找不到zlib库。MySQL已正确安装且可正常运行,安装时的报错日志如下:

pip install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-2.1.1.tar.gz (88 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mysqlclient
Building wheel for mysqlclient (pyproject.toml) ... error
error: subprocess-exited-with-error

× Building wheel for mysqlclient (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [43 lines of output]
mysql_config --version
['8.0.33']
mysql_config --libs
['-L/opt/homebrew/Cellar/mysql/8.0.33/lib', '-lmysqlclient', '-lzlib', '-lzstd', '-L/opt/homebrew/opt/openssl@1.1/lib', '-lssl', '-lcrypto', '-lresolv']
mysql_config --cflags
['-I/opt/homebrew/Cellar/mysql/8.0.33/include/mysql']
ext_options:
library_dirs: ['/opt/homebrew/Cellar/mysql/8.0.33/lib', '/opt/homebrew/opt/openssl@1.1/lib']
libraries: ['mysqlclient', 'zlib', 'resolv']
extra_compile_args: ['-std=c99']
extra_link_args: []
include_dirs: ['/opt/homebrew/Cellar/mysql/8.0.33/include/mysql']
extra_objects: []
define_macros: [('version_info', "(2,1,1,'final',0)"), ('__version__', '2.1.1')]
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-10.9-universal2-cpython-310
creating build/lib.macosx-10.9-universal2-cpython-310/MySQLdb
copying MySQLdb/__init__.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb
copying MySQLdb/_exceptions.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb
copying MySQLdb/connections.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb
copying MySQLdb/converters.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb
copying MySQLdb/cursors.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb
copying MySQLdb/release.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb
copying MySQLdb/times.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb
creating build/lib.macosx-10.9-universal2-cpython-310/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.9-universal2-cpython-310/MySQLdb/constants
running build_ext
building 'MySQLdb._mysql' extension
creating build/temp.macosx-10.9-universal2-cpython-310
creating build/temp.macosx-10.9-universal2-cpython-310/MySQLdb
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch arm64 -arch x86_64 -g -I/opt/homebrew/opt/openssl@3/include -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/opt/homebrew/Cellar/mysql/8.0.33/include/mysql -I/Library/Frameworks/Python.framework/Versions/3.10/include/python3.10 -c MySQLdb/_mysql.c -o build/temp.macosx-10.9-universal2-cpython-310/MySQLdb/_mysql.o -std=c99
clang -bundle -undefined dynamic_lookup -arch arm64 -arch x86_64 -g -L/opt/homebrew/opt/openssl@3/lib -I/opt/homebrew/opt/openssl@3/include build/temp.macosx-10.9-universal2-cpython-310/MySQLdb/_mysql.o -L/opt/homebrew/Cellar/mysql/8.0.33/lib -L/opt/homebrew/opt/openssl@1.1/lib -lmysqlclient -lzlib -lresolv -o build/lib.macosx-10.9-universal2-cpython-310/MySQLdb/_mysql.cpython-310-darwin.so
ld: library not found for -lzlib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for mysqlclient
Failed to build mysqlclient
ERROR: Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects

解决方案

  • 指定zlib库路径:通过环境变量告知编译器zlib的位置,再执行安装

    export LDFLAGS="-L/opt/homebrew/opt/zlib/lib"
    export CPPFLAGS="-I/opt/homebrew/opt/zlib/include"
    # pip安装
    pip install mysqlclient
    # Poetry安装
    poetry add mysqlclient
    
  • 修复mysql_config参数错误:从日志可见mysql_config --libs返回了错误的-lzlib参数(正确应为-lz)

    1. 找到mysql_config位置:which mysql_config(通常为/opt/homebrew/bin/mysql_config)
    2. 编辑该文件,将所有-lzlib替换为-lz,保存后重新执行安装命令
  • 安装预编译wheel包:跳过编译过程,直接安装适配M2的预编译包,先确保pip为最新版

    pip install --upgrade pip
    pip install mysqlclient --only-binary :all:
    

内容的提问来源于stack exchange,提问作者zkop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 08:42:21