我正在尝试按照构建静态链接库来编译 SQLite3 并将其导入到 Python3 中,但是我无法让它工作。我已经从源代码编译,复制了 .c 和 .h 文件,运行
python setup.py build_static build
,但它不起作用。
❯ find . -name "_sqlite3*.so"
./build/lib.macosx-14.0-arm64-cpython-312/pysqlite3/_sqlite3.cpython-312-darwin.so
❯ python3 -c "import sys; print(sys.path)"
['', '/opt/homebrew/Cellar/[email protected]/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python312.zip', '/opt/homebrew/Cellar/[email protected]/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12', '/opt/homebrew/Cellar/[email protected]/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload', '/Users/myusername/Library/Python/3.12/lib/python/site-packages', '/opt/homebrew/lib/python3.12/site-packages', '/opt/homebrew/lib/python3.12/site-packages/pysqlite3-0.5.4-py3.12-macosx-14.0-arm64.egg', '/opt/homebrew/opt/[email protected]/libexec']
但是,然后尝试导入它不起作用:
from pysqlite3 import dbapi2 as sqlite3
Traceback (most recent call last):
File "/Users/username/Sandbox/sqlite3ttt/pysqlite3/a.py", line 1, in <module>
from pysqlite3 import dbapi2 as sqlite3
File "/Users/username/Sandbox/sqlite3ttt/pysqlite3/pysqlite3/__init__.py", line 23, in <module>
from pysqlite3.dbapi2 import *
File "/Users/username/Sandbox/sqlite3ttt/pysqlite3/pysqlite3/dbapi2.py", line 28, in <module>
from pysqlite3._sqlite3 import *
ModuleNotFoundError: No module named 'pysqlite3._sqlite3'
错误是因为当前目录下有一个名为
pysqlite3
的子目录,隐藏了build/lib.macosx-14.0-arm64-cpython-312/pysqlite3
。
在您运行的目录中
find . -name "_sqlite3*.so"
,执行以下命令:
export PYTHONPATH="$PWD/build/lib.macosx-14.0-arm64-cpython-312"
cd /tmp
python3 -c "from pysqlite3 import dbapi2 as sqlite3" # <- should not print error