macos pip install mysqlclient error: command ‘/usr/bin/clang‘ failed with exit code 1

背景

  • 芯片:MacBook Pro M3
  • Mac OS: 14.1
  • Python 3.9.19

执行pip install mysqlclient==1.4.4总是报错,例如:

  • MySQLdb/_mysql.c:1859:9: error: call to undeclared function 'mysql_shutdown'; ISO C99 and later do not support implicit function declarations
  • ld: library 'zstd' not found

错误详细信息
错误一:

  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... 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
  ╰─> [45 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-14-x86_64-cpython-39
      creating build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      copying MySQLdb/__init__.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      copying MySQLdb/_exceptions.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      copying MySQLdb/compat.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      copying MySQLdb/connections.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      copying MySQLdb/converters.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      copying MySQLdb/cursors.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      copying MySQLdb/release.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      copying MySQLdb/times.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb
      creating build/lib.macosx-14-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/__init__.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/CR.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/ER.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb/constants
      copying MySQLdb/constants/FLAG.py -> build/lib.macosx-14-x86_64-cpython-39/MySQLdb/constants
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-14-x86_64-cpython-39
      creating build/temp.macosx-14-x86_64-cpython-39/MySQLdb
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk -I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/openssl@1.1/include -Dversion_info=(1,4,4,'final',0) -D__version__=1.4.4 -I/usr/local/Cellar/mysql/8.3.0_1/include/mysql -I/Users/skyler/Documents/py-env/include -I/usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.macosx-14-x86_64-cpython-39/MySQLdb/_mysql.o
      MySQLdb/_mysql.c:489:9: error: call to undeclared function 'mysql_ssl_set'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
              mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher);
              ^
      MySQLdb/_mysql.c:489:9: note: did you mean 'mysql_close'?
      /usr/local/Cellar/mysql/8.3.0_1/include/mysql/mysql.h:797:14: note: 'mysql_close' declared here
      void STDCALL mysql_close(MYSQL *sock);
                   ^
      MySQLdb/_mysql.c:1650:9: error: call to undeclared function 'mysql_kill'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
          r = mysql_kill(&(self->connection), pid);
              ^
      MySQLdb/_mysql.c:1650:9: note: did you mean 'mysql_ping'?
      /usr/local/Cellar/mysql/8.3.0_1/include/mysql/mysql.h:525:13: note: 'mysql_ping' declared here
      int STDCALL mysql_ping(MYSQL *mysql);
                  ^
      MySQLdb/_mysql.c:1859:9: error: call to undeclared function 'mysql_shutdown'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
          r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT);
              ^
      3 errors generated.
      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

错误二:

      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-14-x86_64-cpython-39
      creating build/temp.macosx-14-x86_64-cpython-39/MySQLdb
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk -I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/readline/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -Wno-implicit-function-declaration -I/usr/local/opt/mysql-client/include -Dversion_info=(1,4,4,'final',0) -D__version__=1.4.4 -I/usr/local/opt/mysql-client/include/mysql -I/Users/skyler/Documents/py-env/include -I/usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.macosx-14-x86_64-cpython-39/MySQLdb/_mysql.o
      clang -bundle -undefined dynamic_lookup -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk -L/usr/local/opt/mysql-client/lib -I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/readline/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -Wno-implicit-function-declaration -I/usr/local/opt/mysql-client/include build/temp.macosx-14-x86_64-cpython-39/MySQLdb/_mysql.o -L/usr/local/opt/mysql-client/lib -lmysqlclient -lz -lzstd -lssl -lcrypto -lresolv -o build/lib.macosx-14-x86_64-cpython-39/MySQLdb/_mysql.cpython-39-darwin.so
      ld: library 'zstd' not found
      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

解决方法

系统需要提前安装必要的lib,例如

  • brew install mysql-client
    • (mysql服务还是可以通过 brew install mysql去使用)
  • brew install openssl@1.1
  • brew install zstd
  • (pip install 是提示ld: library ‘xxx’ not found就安装啥)
# 设置编译依赖
export CPPFLAGS="-I$(brew --prefix openssl@1.1)/include -I$(brew --prefix libffi)/include -I$(brew --prefix readline)/include -I$(brew --prefix zstd)/include"
export CFLAGS="-I$(brew --prefix openssl@1.1)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include -Wno-implicit-function-declaration -I$(brew --prefix zstd)/include" 
export LDFLAGS="-L$(brew --prefix openssl@1.1)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib -L$(brew --prefix gettext)/lib -L$(brew --prefix libffi)/lib -L$(brew --prefix zstd)/lib"
# 执行安装
pip install mysqlclient==1.4.4

按照如此执行可以安装成功。

其他问题

安装好的mysqlclient==1.4.4在执行Django程序的时候会报错

Traceback (most recent call last):
  File "/Users/skyler/Documents/py-env/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/Users/skyler/Documents/py-env/lib/python3.9/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/skyler/Documents/py-env/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_mysql_kill'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
...
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

直接升级pip install -U mysqlclient即可。

本次升级到的版本是mysqlclient==2.2.4

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值