背景
- 芯片: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
错误3
ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
ld: warning: ignoring file /usr/local/opt/openssl@1.1/lib/libssl.dylib, building for macOS-i386 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /usr/local/opt/openssl@1.1/lib/libcrypto.dylib, building for macOS-i386 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /usr/local/lib/libzstd.dylib, building for macOS-i386 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /usr/local/Cellar/mysql/8.0.32/lib/libmysqlclient.dylib, building for macOS-i386 but attempting to link with file built for macOS-x86_64
ld: warning: ignoring file /usr/local/lib/libz.dylib, building for macOS-i386 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libresolv.tbd, missing required architecture i386 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libresolv.tbd (3 slices)
ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, missing required architecture i386 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd (3 slices)
ld: dynamic executables or dylibs must link with libSystem.dylib for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1
解决方法
系统需要提前安装必要的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"
错误3中关于arch的问题
# 检查 Python 的架构
python3.6 -c "import platform; print(platform.architecture())"
根据输出架构选择其中之一设置环境变量。
# x86架构
export ARCHFLAGS="-arch x86_64"
# arm架构
export ARCHFLAGS="-arch arm64"
最后执行安装
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
。