#9120: Improve error reports for exceptions in models.loading.load_app
-----------------------------------------+----------------------------------
Reporter: hvendelbo | Owner: nobody
Status: new | Milestone:
Component: Core framework | Version: 1.0
Keywords: ImportError,load_app,Error: | Stage: Unreviewed
Has_patch: 1 |
-----------------------------------------+----------------------------------
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
--
Ticket URL: <http://code.djangoproject.com/ticket/9120>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---