Opened 9 years ago

Closed 8 years ago

#26618 closed Cleanup/optimization (fixed)

Improve error message when AppConfig.name is invalid

Reported by: Alasdair Nicol Owned by: nobody
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When loading the installed apps, Django will attempt to import the module using AppConfig.name (https://github.com/django/django/blob/e475e849703d937e158e75e7a6d9cb99090857f6/django/apps/config.py#L142):

        # Obtain app name here rather than in AppClass.__init__ to keep
        # all error checking for entries in INSTALLED_APPS in one place.
        try:
            app_name = cls.name
        except AttributeError:
            raise ImproperlyConfigured(
                "'%s' must supply a name attribute." % entry)

        # Ensure app_name points to a valid module.
        app_module = import_module(app_name)

If this fails, the import_module will raise ImportError, however it isn't immediately obvious that the problem is the AppConfig.name attribute.

  File "/Users/anicol/dev/django/v/py3dj19/lib/python3.5/site-packages/django/core/management/__init__.py", line 327, in execute
    django.setup()
  File "/Users/anicol/dev/django/v/py3dj19/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/anicol/dev/django/v/py3dj19/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/anicol/dev/django/v/py3dj19/lib/python3.5/site-packages/django/apps/config.py", line 142, in create
    app_module = import_module(app_name)
  File "/Users/anicol/dev/django/v/py3dj19/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'newapp'

On this Stack Overflow question, the user assumes the problem is in INSTALLED_APPS instead of their AppConfig class.

We could catch the ImportError and raise ImproperlyConfigured instead, asking the user to check AppConfig.name.

Change History (4)

comment:1 by Quentin Fulsher, 9 years ago

This seems like a good idea to me, I made a preliminary pull request here for it: https://github.com/django/django/pull/6632

I'm still not sure where to put tests for it or where to edit the docs but I'll look around.

comment:2 by Quentin Fulsher, 9 years ago

Has patch: set
Needs documentation: set
Needs tests: set
Patch needs improvement: set

comment:3 by Tim Graham, 9 years ago

Component: Error reportingCore (Other)
Needs documentation: unset
Needs tests: unset
Owner: set to nobody
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

I don't think any documentation changes are required.

comment:4 by Tim Graham, 8 years ago

Patch needs improvement: unset
Resolution: fixed
Status: newclosed

In 080dd74e016fbc99d3aaecd36ef932424042b768:

Fixed #26618 -- Improved error message when AppConfig.name is invalid.

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