Code

Opened 6 years ago

Closed 16 months ago

#9120 closed New feature (fixed)

Improve error reports for exceptions in models.loading.load_app

Reported by: hvendelbo Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Normal Keywords: ImportError,load_app,Error:
Cc: dnordberg@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Attachments (2)

9120.load_app.py (1.0 KB) - added by hvendelbo 6 years ago.
load_app method with better error reporting
20090423.diff (619 bytes) - added by lsaffre 5 years ago.

Download all attachments as: .zip

Change History (10)

Changed 6 years ago by hvendelbo

load_app method with better error reporting

comment:1 Changed 5 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

Can you please upload a real patch (see the contributing doc for guidelines) so we can figure out what you're changing here?

comment:2 Changed 5 years ago by lsaffre

hvendelbo, why do you say that "Outputting a stack trace is really too much information"?
Here is a patch that helped me to solve the same problem (import error caused by my code,
occuring during translation.activate('en-us') where import errors are captured).
I don't see why outputting the stack trace is really too much information.

Changed 5 years ago by lsaffre

comment:3 Changed 5 years ago by anonymous

  • Cc dnordberg@… added

comment:4 Changed 3 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:5 Changed 3 years ago by boris.savelev@…

  • Easy pickings unset

comment:6 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:7 Changed 16 months ago by aaugustin

  • Component changed from Core (Other) to Database layer (models, ORM)

comment:8 Changed 16 months ago by akaariai

  • Resolution set to fixed
  • Status changed from new to closed

This seems to be fixed in master, you get an informative exception if your models.py contains invalid code.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.