使用django3.X版本在centos7系统运行抛错
# [django can't find new sqlite version? (SQLite 3.8.3 or later is required (found 3.7.17))](https://stackoverflow.com/questions/55674176/django-cant-find-new-sqlite-version-sqlite-3-8-3-or-later-is-required-found)
刚刚开始抛当前版本不支持。我升级sqlite3的版本后,验证了 sqlite3 的版本为我安装的版本。也验证了python3中导入的 sqlite3 版本 也是我安装的版本。继续运行django项目, 继续抛以下错误:
"/usr/local/python3/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py" 602L, 25741C written
fn(*args, **kwargs)
File "/usr/local/python3/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check_migrations()
File "/usr/local/python3/lib/python3.8/site-packages/django/core/management/base.py", line 459, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/usr/local/python3/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/python3/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/usr/local/python3/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/python3/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/usr/local/python3/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/usr/local/python3/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/python3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/usr/local/python3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/usr/local/python3/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/python3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/usr/local/python3/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/python3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/usr/local/python3/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/python3/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/python3/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/python3/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 215, in get_new_connection
create_deterministic_function('django_date_extract', 2, _sqlite_datetime_extract)
django.db.utils.NotSupportedError: deterministic=True requires SQLite 3.8.3 or higher
以为是动态链接库没有更新 又重新安装了一把python3.8、发现结果还是一样不能运行成功
升级之后由一个不能被支持的参数引发NotSupportError,说明django虽然获取到了>=3.8.3版本的sqlite,但是部分方法仍然不能支持新特性、
如此这般,只能走另外一条途径解决问题。不依赖系统提供的sqlite3 使用 pysqlite3
和 pysqlite3-binary
库来操作。首先需要安装这两个库
pip3 install pysqlite3
pip3 install pysqlite3-binary
修改db代码导入库
vi /usr/local/python3/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py
# 修改内容
# from sqlite3 import dbapi2 as Database (注释掉这段)
from pysqlite3 import dbapi2 as Database # 启用pysqlite3
# :wq 保存即可
然后可运行 python3 manage.py runserver 即成功运行