Opened 12 years ago

Closed 12 years ago

#20428 closed Uncategorized (duplicate)

ImportError is swallowed while importing settings

Reported by: giovannibajo Owned by: nobody
Component: Uncategorized Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In django.management.get_commands, there is the following code:

        # Find the installed apps
        try:
            from django.conf import settings
            apps = settings.INSTALLED_APPS
        except (AttributeError, EnvironmentError, ImportError):
            apps = []

The problem with this code is that any ImportError raised by settings.py (eg: a missing third-party dependency) are swallowed, and this then leads to very hard-to-diagnose bugs because the settings module is half-loaded into memory, so a part of symbols might be missing.

The bug is that Django treats ImportError as an indication that settings.py does not exist, while in reality it is only a possibility; the other is that settings.py contains code that triggers ImportError.

My personal fix is to change the code as follows:

        # Find the installed apps
        from django.conf import settings
        try:
            apps = settings.INSTALLED_APPS
        except AttributeError:
            apps = []

but I don't know whether it is the right thing. I assume the code was meant to somehow work even if the settings module is missing, while this way I'm basically forcing its existence. By changing the code, I know get this exception:

    ImportError: Could not import settings 'myapp.settings' (Is it on sys.path?): No module named django_hstore

which reports the original exception, the missing dependency, and thus lets me fix it.

Change History (1)

comment:1 by Claude Paroz, 12 years ago

Resolution: duplicate
Status: newclosed

Thanks for the report, but that has been fixed some time ago (see #18545 and #18845).

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