Django/Sqlite3 Python 虚拟环境中的模块加载路径

Django/Sqlite3 Module Load Path in Python Virtual Environment

我使用最新的 Python 版本 (3.8.3) 在 Django (3.0.8) 中开发了一个网站。我在安装了 Python 3.6.9 的 Unix 服务器上工作,其中包括 Sqlite 版本 3.7.17。 Django 显然需要 Sqlite 3.8 或更高版本。

因此,我按照此处的指南编译了最新 Python 的本地副本:https://www.a2hosting.com/kb/developer-corner/python/using-a-newer-version-of-python

我如上所述设置了一个虚拟环境,一切运行顺利,除了 Python 仍在使用旧的 Sqlite 版本。经过数小时的工作试图弄清楚这一切,我感到很困惑。

我可以在标准环境下通过命令行访问Python和Sqlite3:

-bash-4.2$ python --version
Python 3.6.9

-bash-4.2$ python
>>> import sqlite3, inspect
>>> sqlite3.sqlite_version
'3.7.17'

>>> inspect.getfile(sqlite3)
'opt/rh/rh-python36/root/usr/lib64/python3.6/sqlite3/__init__.py'

在虚拟环境中:

-bash-4.2$ source bin/venv/bin/activate
(venv) -bash-4.2$ python --version
Python 3.8.3

-bash-4.2$ python
>>> import sqlite3, inspect
>>> sqlite3.sqlite_version
'3.7.17'

>>> inspect.getfile(sqlite3)
'users/.../lib/python3.8/sqlite3/__init__.py

因此,尽管 运行 Python 3.8.3 并在虚拟环境中指向正确的库(据我所知),sqlite 版本仍然与标准版本相同环境。任何建议将不胜感激!

SQlite3python 安装的一部分,不是外部库,即您不能使用像 pip 这样的包管理器进行升级。对于每个 python 版本,您将拥有不同的 Sqltie 版本。但是,如果您替换了 dll 文件,则情况并非如此。 我看到一些用户在 windows 上这样做,对我来说这是一个相当冒险的方法。

如果你已经替换了 dlls 然后尝试还原它并且它的输出应该不同于 python 3.6.9

-bash-4.2$ python
>>> import sqlite3, inspect
>>> sqlite3.sqlite_version
'3.7.17'  # would be different version here

您也可以(如果需要)从版本 >=Sqlite 3.8here 下载 sqlite3.dll(预编译的二进制)文件。然后将此文件复制到python安装目录下的DLLs文件夹中。您可能需要备份旧的 sqlite3.dll 文件,以防万一您决定还原。

好的,Laenka-Oss提供的答案稍作调整就解决了我的问题:django can't find new sqlite version? (SQLite 3.8.3 or later is required (found 3.7.17))

从源安装 Sqlite:

cd ~
wget https://www.sqlite.org/2020/sqlite-autoconf-3320300.tar.gz
tar zxvf sqlite-autoconf-3290000.tar.gz
cd sqlite-autoconf-3290000

./configure --prefix=$HOME/opt/sqlite --disable-dynamic-extensions --enable-static --disable-shared
make && make install

通过将这些行添加到您的 .bash_profile:

来更新库路径
export PATH=$HOME/opt/sqlite/bin:$PATH
export LD_LIBRARY_PATH=$HOME/opt/sqlite/lib
export LD_RUN_PATH=$HOME/opt/sqlite/lib

从源安装 Python:

cd ~
wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tar.xz
tar xJf Python-3.8.3.tar.xz
cd Python-3.8.3
./configure --prefix=$HOME/opt
make && make install

现在更新 Python 路径,将其添加到 .bash_profile:

的末尾
export PATH=$HOME/opt/Python-3.8.3/bin:$PATH

检查是否一切正常:

source .bash_profile
python3 --version
Python 3.8.3

python3
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.32.3'

如果遇到“SqLite header 和版本不匹配:...”错误,请确保 运行 source .bash_profile 或重新启动连接。如果这不起作用,请仔细检查 sqlite 安装是否使用了上面显示的命令。

您的 .bash_profile 应如下所示:

export PATH=$HOME/opt/sqlite/bin:$PATH
export LD_LIBRARY_PATH=$HOME/opt/sqlite/lib
export LD_RUN_PATH=$HOME/opt/sqlite/lib
export PATH=$HOME/opt/Python-3.8.3/bin:$PATH