Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#32197 closed Cleanup/optimization (fixed)

Misleading error when loading a database backend fails with ImportError

Reported by: Tim Graham Owned by: Hasan Ramezani
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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..

Change History (6)

comment:1 by Mariusz Felisiak, 4 years ago

Triage Stage: UnreviewedAccepted

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

comment:2 by Hasan Ramezani, 4 years ago

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:3 by Hasan Ramezani, 4 years ago

Has patch: set
Last edited 4 years ago by Tim Graham (previous) (diff)

comment:4 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 4cce1d13:

Fixed #32197 -- Improved error message when loading a database backend fails.

comment:6 by HamzaTarar, 3 years ago

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

Note: See TracTickets for help on using tickets.
Back to Top