[Django] #32197: Misleading error when loading a database backend fails with ImportError

21 views
Skip to first unread message

Django

unread,
Nov 14, 2020, 8:43:01 PM11/14/20
to django-...@googlegroups.com
#32197: Misleading error when loading a database backend fails with ImportError
-------------------------------------+-------------------------------------
Reporter: Tim | Owner: nobody
Graham |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
For example, in the following case, `django_cockroachdb` is installed but
has an import error itself.
{{{
Traceback (most recent call last):
File "/home/tim/code/django/django/db/utils.py", line 110, in
load_backend
return import_module('%s.base' % backend_name)
File
"/home/tim/.virtualenvs/django39/lib/python3.9/importlib/__init__.py",
line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in
_call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in
_call_with_frames_removed
File "/home/tim/code/cockroach-django/django_cockroachdb/__init__.py",
line 2, in <module>
from .lookups import patch_lookups
File "/home/tim/code/cockroach-django/django_cockroachdb/lookups.py",
line 1, in <module>
from django.db.models.fields.json import HasKeyLookup, KeyTransform
ModuleNotFoundError: No module named 'django.db.models.fields.json'

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

Traceback (most recent call last):
File "/home/tim/code/mysite/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/tim/code/django/django/core/management/__init__.py", line
381, in execute_from_command_line
utility.execute()
File "/home/tim/code/django/django/core/management/__init__.py", line
357, in execute
django.setup()
File "/home/tim/code/django/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/tim/code/django/django/apps/registry.py", line 114, in
populate
app_config.import_models()
File "/home/tim/code/django/django/apps/config.py", line 211, in
import_models
self.models_module = import_module(models_module_name)
File
"/home/tim/.virtualenvs/django39/lib/python3.9/importlib/__init__.py",
line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in
_call_with_frames_removed
File "/home/tim/code/mysite/polls/models.py", line 12, in <module>
class Question(models.Model):
File "/home/tim/code/django/django/db/models/base.py", line 117, in
__new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home/tim/code/django/django/db/models/base.py", line 321, in
add_to_class
value.contribute_to_class(cls, name)
File "/home/tim/code/django/django/db/models/options.py", line 204, in
contribute_to_class
self.db_table = truncate_name(self.db_table,
connection.ops.max_name_length())
File "/home/tim/code/django/django/db/__init__.py", line 28, in
__getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/tim/code/django/django/db/utils.py", line 201, in
__getitem__
backend = load_backend(db['ENGINE'])
File "/home/tim/code/django/django/db/utils.py", line 121, in
load_backend
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: 'django_cockroachdb' isn't an
available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
}}}
I'm not sure there's anything that could be done besides rephrasing the
error message to say something like this:

'django_cockroachdb' isn't an available database backend or couldn't be
imported. Check the above exception.

In this case, "Try using..." isn't so helpful either. Perhaps we could
only include that if `backend_name` begins with `django.`.

--
Ticket URL: <https://code.djangoproject.com/ticket/32197>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 15, 2020, 2:42:09 PM11/15/20
to django-...@googlegroups.com
#32197: Misleading error when loading a database backend fails with ImportError
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Unreviewed => Accepted


Comment:

I agree that rephrasing the error message is all we can do.

--
Ticket URL: <https://code.djangoproject.com/ticket/32197#comment:1>

Django

unread,
Nov 15, 2020, 3:28:33 PM11/15/20
to django-...@googlegroups.com
#32197: Misleading error when loading a database backend fails with ImportError
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Hasan
Type: | Ramezani
Cleanup/optimization | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* owner: nobody => Hasan Ramezani
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32197#comment:2>

Django

unread,
Nov 15, 2020, 4:53:56 PM11/15/20
to django-...@googlegroups.com
#32197: Misleading error when loading a database backend fails with ImportError
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Hasan
Type: | Ramezani
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32197#comment:3>

Django

unread,
Nov 16, 2020, 12:07:24 AM11/16/20
to django-...@googlegroups.com
#32197: Misleading error when loading a database backend fails with ImportError
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Hasan
Type: | Ramezani
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32197#comment:4>

Django

unread,
Nov 16, 2020, 9:10:27 AM11/16/20
to django-...@googlegroups.com
#32197: Misleading error when loading a database backend fails with ImportError
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Hasan
Type: | Ramezani
Cleanup/optimization | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"4cce1d13cfe9d8e56921c5fa8c61e3034dc8e20c" 4cce1d13]:
{{{
#!CommitTicketReference repository=""
revision="4cce1d13cfe9d8e56921c5fa8c61e3034dc8e20c"
Fixed #32197 -- Improved error message when loading a database backend
fails.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32197#comment:5>

Django

unread,
May 24, 2021, 11:34:18 AM5/24/21
to django-...@googlegroups.com
#32197: Misleading error when loading a database backend fails with ImportError
-------------------------------------+-------------------------------------
Reporter: Tim Graham | Owner: Hasan
Type: | Ramezani
Cleanup/optimization | Status: closed
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by HamzaTarar):

I'm facing a 32197 error in Django anybody tells me how to resolve this
problem

--
Ticket URL: <https://code.djangoproject.com/ticket/32197#comment:6>

Reply all
Reply to author
Forward
0 new messages