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

在venv中安装mysqlclient时如何解决‘library not found for -lzlib’错误?

MacOS Ventura 13.4下安装mysqlclient失败的解决办法

问题场景

在MacOS Ventura 13.4系统、MySQL 8.0.33版本环境中,尝试在venv虚拟环境通过requirements.txt安装mysqlclient时失败。已尝试Homebrew重装mysqlclient、openssl等依赖,更新Xcode命令行工具,均未解决。

错误日志

pip3 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/usr/local/opt/mysql-client/lib', '-lmysqlclient', '-lzlib', '-lzstd', '-L/usr/local/opt/openssl@1.1/lib', '-lssl', '-lcrypto', '-lresolv']
      mysql_config --cflags
      ['-I/usr/local/opt/mysql-client/include/mysql']
      ext_options:
        library_dirs: ['/usr/local/opt/mysql-client/lib', '/usr/local/opt/openssl@1.1/lib']
        libraries: ['mysqlclient', 'zlib', 'resolv']
        extra_compile_args: ['-std=c99']
        extra_link_args: []
        include_dirs: ['/usr/local/opt/mysql-client/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-x86_64-cpython-37
      creating build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb
      copying MySQLdb/__init__.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb
      copying MySQLdb/_exceptions.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb
      copying MySQLdb/connections.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb
      copying MySQLdb/converters.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb
      copying MySQLdb/cursors.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb
      copying MySQLdb/release.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb
      copying MySQLdb/times.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb
      creating build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb/constants
      copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb/constants
      copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb/constants
      copying MySQLdb/constants/CR.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb/constants
      copying MySQLdb/constants/ER.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb/constants
      copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb/constants
      copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb/constants
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-10.9-x86_64-cpython-37
      creating build/temp.macosx-10.9-x86_64-cpython-37/MySQLdb
      gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/usr/local/opt/mysql-client/include/mysql -I/Users/ankikhat/Desktop/camdb_updates/venv/include -I/Library/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c MySQLdb/_mysql.c -o build/temp.macosx-10.9-x86_64-cpython-37/MySQLdb/_mysql.o -std=c99
      gcc -bundle -undefined dynamic_lookup -arch x86_64 -g build/temp.macosx-10.9-x86_64-cpython-37/MySQLdb/_mysql.o -L/usr/local/opt/mysql-client/lib -L/usr/local/opt/openssl@1.1/lib -lmysqlclient -lzlib -lresolv -o build/lib.macosx-10.9-x86_64-cpython-37/MySQLdb/_mysql.cpython-37m-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/gcc' 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

解决方法

方法1:指定zlib路径

安装前设置环境变量,指定zlib的库和头文件路径,再执行安装:

# 系统默认zlib路径
export LDFLAGS="-L/usr/lib"
pip install mysqlclient

# 如果是Homebrew安装的zlib,用这个
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
pip install mysqlclient

方法2:修正mysql_config配置

  1. 找到mysql_config位置:which mysql_config,例如输出/usr/local/bin/mysql_config
  2. 编辑该文件,找到libs=开头的行,把-lzlib替换成-lz
  3. 保存后重新执行pip install mysqlclient

方法3:安装预编译wheel包

跳过源码编译,直接安装预编译版本:

pip install --only-binary :all: mysqlclient

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:17:35