首页 > 解决方案 > Python虚拟环境中的Django/Sqlite3模块加载路径

问题描述

我使用最新的 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 版本仍然与标准环境相同。任何建议将不胜感激!

标签: pythondjangosqlitevirtualenv

解决方案


好的,Laenka-Oss 提供的答案通过细微的调整解决了我的问题:django 找不到新的 sqlite 版本?(需要 SQLite 3.8.3 或更高版本(找到 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

现在通过将其添加到 .bash_profile 的末尾来更新 Python 路径:

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 标头和版本不匹配:...”错误,请确保您运行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

推荐阅读