同一服务器上多个版本的 Sqlite3 for Python [英] Multiple versions of Sqlite3 for Python on the same server
问题描述
在 Linux 服务器上,我有一些 Python 脚本使用内置的 sqlite3
模块(+ 一些从源代码构建的 Sqlite 扩展,详见 在 Debian 上升级 Python 的 sqlite3).
On a Linux server, I have some Python scripts using the built-in sqlite3
module (+ some Sqlite extensions built from source, as detailed in Upgrade Python's sqlite3 on Debian).
对于另一个 Python 脚本,我需要一个 较新版本的 Sqlite 共享库,而不是系统上已有的库.原因:我需要 Sqlite 高于 3.25.0 的窗口函数.
For another Python script, I need a newer version of the Sqlite shared library than the one I already have on the system. Reason: I need Sqlite higher than 3.25.0 for Window Functions.
如果我安装它从这里的源代码并执行 make
和 make install
,它可能会覆盖服务器上此库的先前版本,并可能破坏使用它的其他操作系统工具.
If I install it from source here and do make
and make install
, it will probably overwrite previous versions of this library on the server, and could potentially break other OS tools using it.
您如何处理拥有多个版本的 Sqlite 共享库的一般问题?
我认为 Python 虚拟环境不能用于此上下文,或者有可能吗?
I don't think Python virtual environments can be used for this context, or would it be possible?
注意:pip3 install --upgrade sqlite3
不存在:我们不能像这样升级 Python 的内置 sqlite3
包.顺便说一句,我们可能不应该这样做,因为它可能会破坏一些使用 Python + sqlite3 的操作系统工具.
Note: pip3 install --upgrade sqlite3
does not exist: we cannot upgrade Python's built-in sqlite3
package like this. And by the way we probably should not, since it could break some OS tools using Python + sqlite3.
推荐答案
这非常棘手,需要对脚本进行一些代码更改.
This is very tricky and will need a little code change in your scripts.
做什么:
首先,检查 python 附带的 sqlite3 library 版本以防万一:
python -c "import sqlite3; print(sqlite3.connect(':memory:').execute('SELECT sqlite_version();').fetchall())
在我的电脑(python 3.8,windows)中,输出是 [('3.35.5',)]
这意味着 python 有 sqlite 3.35.5 库.我的系统中没有安装 sqlite:这是 python3.8 附带的库.
In my computer (python 3.8, windows) the output is [('3.35.5',)]
which means python has the sqlite 3.35.5 library. I have no sqlite installed in my system: this is the library that comes with python3.8.
如果你的 python sqlite3 库不是你需要的 :-( 你有另一种选择:你可以使用 pysqlite3 而不是 sqlite3
标准库.在这种情况下:
IF your python sqlite3 library is not the one you need :-( you have an alternative: you can use the pysqlite3 instead of the sqlite3
standard library. In this case:
- 您需要使用与您要使用的版本相匹配的 Sqlite3合并"自行构建 pysqlite3 库(稍后会详细介绍).
- 您需要安装该库,然后...
- 您需要更改 Python 脚本导入
import pysqlite3 as sqlite3 # 而不是 sqlite3
好的,什么是合并"以及如何构建 pysqlite3?
Ok, what is the 'amalgamation` and how to build pysqlite3?
合并 是一个 .c 文件中的整个 sqlite3 库(带有 sqlite3.h 文件).您可以从 sqlite3 下载页面获取:sqlite3.36 amalgamation.
The amalgamation is the whole sqlite3 library in just one .c file (with the sqlite3.h file). You can get it from the sqlite3 download page: sqlite3.36 amalgamation.
合并后,按照说明构建静态pysqlite3,并安装包.
Once you have the amalgamation, follow the instructions to build statically pysqlite3, and install the package.
现在您可以在代码中使用 pysqlite3.
Now you can use pysqlite3 in your code.
这篇关于同一服务器上多个版本的 Sqlite3 for Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!