﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
9120	Improve error reports for exceptions in models.loading.load_app	Henrik Vendelbo	nobody	"If you old attributes in your model fields or anything else that will raise an exception when trying to load the application models you may get very little error information. In my case all I got was the following:

$ ./manage.py test
Error: cannot import name settings

The exception message is correct, but it didn't really tell me anything useful. If I tried importing settings in a Python shell it worked fine. And if I tried to run any manage.py command, it would fail with the same error message. What was worse, was that I didn't know where the error was caught, so I was really stuck.  The real problem is much more clear with a stack trace:

{{{
Traceback (most recent call last):
  File "".../django/core/management/base.py"", line 88, in execute
    translation.activate('en-us')
  File "".../django/utils/translation/__init__.py"", line 74, in activate
    return real_activate(language)
  File "".../django/utils/translation/__init__.py"", line 44, in delayed_loader
    return g['real_%s' % caller](*args, **kwargs)
  File "".../django/utils/translation/trans_real.py"", line 209, in activate
    _active[currentThread()] = translation(language)
  File "".../django/utils/translation/trans_real.py"", line 198, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "".../django/utils/translation/trans_real.py"", line 183, in _fetch
    app = __import__(appname, {}, {}, [])
  File "".../mailer/__init__.py"", line 1, in <module>
    import mailer.admin
  File "".../mailer/admin.py"", line 13, in <module>
    admin.site.register(Message, MessageAdmin)
  File "".../django/contrib/admin/sites.py"", line 76, in register
    validate(admin_class, model)
  File "".../django/contrib/admin/validation.py"", line 22, in validate
    models.get_apps()
  File "".../django/db/models/loading.py"", line 103, in get_apps
    self._populate()
  File "".../django/db/models/loading.py"", line 57, in _populate
    self.load_app(app_name, True)
  File "".../django/db/models/loading.py"", line 73, in load_app
    mod = __import__(app_name, {}, {}, ['models'])
  File "".../retail/models.py"", line 10, in <module>
    import tagging
  File "".../tagging/__init__.py"", line 3, in <module>
    from tagging.managers import ModelTaggedItemManager, TagDescriptor
  File "".../tagging/managers.py"", line 8, in <module>
    from tagging.models import Tag, TaggedItem
  File "".../tagging/models.py"", line 16, in <module>
    from tagging import settings
ImportError: cannot import name settings
}}}

Outputting a stack trace is really too much information. The reason all commands fail is that the translation is changed to en-us when executing a command, so settings must be imported which in turn loads models.

I think that a more general improvement could be made, but I think it's more important to minimise the pain for people switching to 1.0

I propose a try-catch around the import call in load_app, which instead would have given me this output:

$ ./manage.py test
Error: Failed to load models for retail, cannot import name settings in ../tagging/models.py line 16

"	New feature	closed	Database layer (models, ORM)	1.0	Normal	fixed	ImportError,load_app,Error:	dnordberg@…	Design decision needed	1	0	0	0	0	0
