自制软件 python@2 和 python 提供损坏的 sqlite3

homebrew python@2 and python provides broken sqlite3

我正在使用最新版本的 macOS 和 homebrew,brew doctor 没有发现任何问题。我只是使用 brew install python, brew install python@2 来获取最新版本的 python.

当我键入 python -c "import sqlite3" 时,我收到以下错误消息:

python2.7 -c "import sqlite3"
130 ↵ Traceback (most recent call last): File "", line 1, in File "/usr/local/Cellar/python@2/2.7.15_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sqlite3/init.py", line 24, in from dbapi2 import * File "/usr/local/Cellar/python@2/2.7.15_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sqlite3/dbapi2.py", line 28, in from _sqlite3 import * ImportError: dlopen(/usr/local/Cellar/python@2/2.7.15_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_sqlite3.so, 2): Symbol not found: _sqlite3_enable_load_extension Referenced from: /usr/local/Cellar/python@2/2.7.15_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_sqlite3.so Expected in: /usr/lib/libsqlite3.dylib in /usr/local/Cellar/python@2/2.7.15_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_sqlite3.so

python -c "import sqlite3"
1 ↵ Traceback (most recent call last): File "", line 1, in File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sqlite3/init.py", line 23, in from sqlite3.dbapi2 import * File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sqlite3/dbapi2.py", line 27, in from _sqlite3 import * ImportError: dlopen(/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_sqlite3.cpython-37m-darwin.so, 2): Symbol not found: _sqlite3_enable_load_extension Referenced from: /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_sqlite3.cpython-37m-darwin.so Expected in: /usr/lib/libsqlite3.dylib in /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_sqlite3.cpython-37m-darwin.so

可能导致问题的原因是什么?我尝试下载 python 源代码并编译它,并将 _sqlite3.so 或 _sqlite3.cpython-37m-darwin.so 文件移动到 brew 安装文件夹中,一切正常。 brew 会忘记配方中的某些东西吗?除了从源码编译.so文件,手动解决问题还能怎么办?

我终于找到了漏洞所在。 我所有的 macOS 设备(包括 2 个 Pros 和 1 个 Air)都共享同一个 .zshrc 文件,出于某种原因,我添加了一个 export DYLD_LIBRARY_PATH="/Users/myname/Library/Developer/Xcode/iOS DeviceSupport/10.0.1 (14A403)/Symbols/usr/lib/:/usr/lib/" 这破坏了 python sqlite 共享库文件的构建,最近为 sqlite 添加了一个名为“_sqlite3_enable_load_extension”的功能。

当我将 DYLD_LIBRARY_PATH 删除到过时的目录,然后 brew 重新安装 python 一切正常。

顺便说一下,brew config 和 brew doctor 没有提供关于 DYLD_LIBRARY_PATH 的信息。下次我会遵守规则并提供这些信息。 问题已解决!