我正在设置一台新的 M2 MacBook,似乎遇到了此处描述的 M1 问题:
https://github.com/PyMySQL/mysqlclient/issues/496
(tl;dr:pip安装的MySQLdb共享对象库和brew安装的mysqlclient dylib不一致,显然是由于x86/arm架构不匹配,导致两者所需的符号不通用。)
我已经为此苦恼了一个星期了。我已经尝试了问题讨论中的多种解决方法,但没有找到任何适用于我的机器的方法。我不太熟悉问题讨论中的低级库架构主题,并且希望我忽略了一些东西。线程中的 arch 安装和执行有很多排列。我尝试应用许多报告的解决方法但没有成功,但我觉得我在没有清楚地了解我所看到的内容的情况下就在刺探它。
python 是:
$ file $(which python)
/Users/sbrown/.virtualenvs/mypkg/bin/python: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64:Mach-O 64-bit executable arm64]
/Users/sbrown/.virtualenvs/mypkg/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64
/Users/sbrown/.virtualenvs/mypkg/bin/python (for architecture arm64): Mach-O 64-bit executable arm64
brew 安装的 libmysqlclient 是:
$ file /opt/homebrew/opt/mysql-client/lib/libmysqlclient.21.dylib
/opt/homebrew/opt/mysql-client/lib/libmysqlclient.21.dylib: Mach-O 64-bit dynamically linked shared library arm64
和
pip install mysqlclient
导致:
$ file ~/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so
/Users/sbrown/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit bundle x86_64] [arm64:Mach-O 64-bit bundle arm64]
/Users/sbrown/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
/Users/sbrown/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so (for architecture arm64): Mach-O 64-bit bundle arm64
GitHub 线程有多个评论引用查看
symbols <library file> | grep mysql_affected_rows
来确定文件是 arm64 还是 x86,但没有人解释如何从输出中判断。我在一台较旧的 x86 MacBook 上检查了所有这些工作,并看到了如下所示的相同输出,这与 GitHub 线程中显示的输出一致。
❓ 人们真的可以从
symbols
的输出中了解有关架构的更多信息吗?file
❓ 另外,由于上面的几个文件都是通用架构,那么什么决定了选择哪种架构呢?在多次尝试解决方法之后,我尝试了下面显示的“canary”命令,在两种风格的 arch 下运行 python,但都失败了:
$ symbols /opt/homebrew/opt/mysql-client/lib/libmysqlclient.21.dylib | grep mysql_affected_rows
0x0000000000011c98 ( 0x8) mysql_affected_rows [FUNC, EXT, NameNList, MangledNameNList, Merged, NList, FunctionStarts]
$ symbols ~/.virtualenvs/llatitude/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so | grep mysql_affected_rows
0x0000000000006dcc ( 0xc) DYLD-STUB$$mysql_affected_rows [DYLD-STUB, LENGTH, NameNList, MangledNameNList, NList]
❓上面的错误是否表明virtualenv中的.so中没有找到该符号?或者该共享对象正在调用其他外部库但找不到符号?
$ arch -arm64 python -c 'import MySQLdb'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/sbrown/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/__init__.py", line 17, in <module>
from . import _mysql
ImportError: dlopen(/Users/sbrown/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so, 0x0002): symbol not found in flat namespace '_mysql_affected_rows'
$ arch -x86_64 python -c 'import MySQLdb'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/sbrown/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/__init__.py", line 17, in <module>
from . import _mysql
ImportError: dlopen(/Users/sbrown/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so, 0x0002): symbol not found in flat namespace '_mysql_affected_rows'
显示:
otool -tV
我忽略的这里有一条明显的路径吗?在我看来,这种基本的东西目前不太可能从根本上被破坏,但我不确定下一步该去哪里。
$ otool -tV ~/.virtualenvs/mypkg/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so | grep mysql_affected_rows
0000000000005711 callq 0x70b8 ## symbol stub for: _mysql_affected_rows
0000000000004cc0 bl 0x6dcc ; symbol stub for: _mysql_affected_rows
使用 no-cache-dir 很重要,这让我很困惑,因为我正确设置了标志,但它使用的是来自缓存的相同的borked构建。
您可以使用otool验证:
export MYSQLCLIENT_CFLAGS="-I$(brew --prefix mysql-client)/include/mysql"
export MYSQLCLIENT_LDFLAGS="-L$(brew --prefix mysql-client)/lib"
pip install --no-cache-dir mysqlclient
如果它有效,那么你应该看到 libmysqlclient.xx.lib,当我收到
otool -L .venv/lib/python3.10/site-packages/MySQLdb/_mysql*.so
.venv/lib/python3.10/site-packages/MySQLdb/_mysql.cpython-310-darwin.so:
/opt/homebrew/opt/mysql-client/lib/libmysqlclient.24.dylib (compatibility version 24.0.0, current version 24.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)
错误时,libmysqlclient 没有显示在 otool 输出中。