如何修复错误:django.db.utils.NotSupportedError:不支持URI

时间:2018-03-26 20:31:03

标签: python django linux amazon-web-services sqlite

在Linux机器的我的Django项目中(在AWS中)我正在使用:

  • Python 3.5.1
  • Django 1.11.7

我为我的项目创建了虚拟环境,并且所有依赖项都在那里完美安装。对于我使用 sqlite3 的数据库。请参阅下面的版本详细信息。

>>>import sqlite3
>>>sqlite3.version
'2.6.0'
>>>sqlite3.sqlite_version_info
(3, 7, 17)

settings.py 中, DATABASES 部分如下所示:

DATABASES = {
   'default': {
      'ENGINE': 'django.db.backends.sqlite3',
      'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
   }
}

在运行runserver时,我得到一个例外,因为 sqlite3.NotSupportedError:不支持URI ,这会产生错误 django.db.utils.NotSupportedError:不支持的URI ,我无法修复。

我浏览了djangoproject.comgoogle.forum等帖子,但仍无法理解此错误的原因。我也试图做 python manage.py makemigrations ,但同样的错误也来了。

注意:在Windows机器上,我的项目运行正常。

请参阅下面的追溯:

# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function check_errors. 
<locals>.wrapper at 0x7f07ff09c2f0>
Traceback (most recent call last):
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/tech/poc/env/lib/python3.5/site- packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 198, in get_new_connection
    conn = Database.connect(**conn_params)
  sqlite3.NotSupportedError: URIs not supported

  The above exception was the direct cause of the following exception:

  Traceback (most recent call last):
  File "/tech/poc/env/lib/python3.5/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/tech/poc/env/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
    self.check_migrations()
  File "/tech/poc/env/lib/python3.5/site-packages/django/core/management/base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/loader.py", line 209, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/tech/poc/env/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 198, in get_new_connection
    conn = Database.connect(**conn_params)
django.db.utils.NotSupportedError: URIs not supported

请帮助理解并修复此错误。此问题在堆栈中没有相关帖子,也没有在谷歌中找到任何相关内容。

3 个答案:

答案 0 :(得分:0)

检查BASE_DIR设置中指定的路径。

答案 1 :(得分:0)

1。您必须先检查sqlite3版本。应该是3.X 我的配置是Python 3.7django 2.1.7sqlite3。 您可以按照以下方式检查sqlite3版本: >>import sqlite3

>>sqlite3.version '2.6.0' >>> sqlite3.sqlite_version_info (3,6,20)`

  1. 签入"/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py" 寻找行“ kwargs.update({'check_same_thread': False, 'uri': False}) 如果'uri'为True,则以root身份将其更改为False。 更改代码后,重新启动计算机并运行python runserver migrate 这样可以解决您的问题。

此外,您可以尝试在SETTINGS.py文件中使用硬编码的db.sqli3文件名来更改DATABASE ['NAME']。

答案 2 :(得分:0)

我遇到了类似的问题。事实证明Django 2.1与sqlite 3.6.20不兼容。降级到2.0.x版本,您应该会很好。该问题已经提出,并被标记为“已关闭的错误修正” here