将内置的 sqlite3 模块复制到另一个模块中,以使用更新版本的 sqlite3 共享库

问题描述

我想使用最新版本的 sqlite3 共享库,直接从 https://www.sqlite.org 的源代码编译而来。

用新编译的库覆盖 /usr/lib/x86_64-linux-gnu/libsqlite3.solibsqlite3.so.0.8.6 似乎是一个坏主意,因为它可能会破坏一些操作系统工具 - 这些工具使用 Python 和标准 sqlite3 模块并且不喜欢新的此共享库的版本。

问题:如何将 stdlib Python 模块 sqlite3 复制到名为 sqlite3custom 的模块中,而不是使用 /usr/lib/python3.5/lib-dynload/_sqlite3.cpython-35m-x86_64-linux-gnu.so 而是使用 /path/to/my/custom/sqlite.so

然后我会在我的代码中使用与 import sqlite3custom 完全相同的 API 的 sqlite3,这样可以避免接触到内置的 sqlite3 模块。

好处:我们甚至可以在同一代码中同时使用两者,以进行比较:

import sqlite3
print(sqlite3.sqlite_version)        # 3.23.1
import sqlite3custom
print(sqlite3custom.sqlite_version)  # 3.36 (2021-06-18),compiled from source 

注意:这与 Upgrade Python's sqlite3 on DebianMultiple versions of Sqlite3 for Python on the same server 有关联但略有不同。

解决方法

我认为您不能同时使用两个版本的 sqlite3 库。 但是,要在您的 python 代码中使用非系统库,您只需更新 LD_LIBRARY_PATH (将包含您的版本的文件夹放在具有系统版本的文件夹之前) 根据您与 sqlite3 的交互方式,您可能需要类似地更新 PATH