Opened 11 years ago

Closed 9 years ago

#10706 closed Bug (fixed)

Incorrect error from sql when app fails to load

Reported by: Glenn Maynard Owned by: nobody
Component: Core (Management commands) Version: master
Severity: Normal Keywords:
Cc: xiong.chiamiov@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


./ sql stomp
Error: App with label stomp could not be found. Are you sure your INSTALLED_APPS setting is correct?

This is an incorrect error when the app is found but fails to import. The actual error is never displayed, which is hard to diagnose. If I add some diagnostics in django/db/models/ load_app to print the exception, the problem becomes obvious:

./ sqlall stomp
cannot import name TestModel
Error: App with label stomp could not be found. Are you sure your INSTALLED_APPS setting is correct?

It should probably just pass the exception up and show a trace. I've attached a patch to do this, but it causes errors when running some tests:

Error while importing humanize:  File "./", line 134, in django_tests
    mod = load_app(model_label)
  File "/home/glenn/django/django/db/models/", line 74, in load_app
    models = import_module('.models', app_name)
  File "/home/glenn/django/django/utils/", line 35, in import_module
ImportError: No module named models

for humanize, syndication, sitemaps, databrowse, admindocs and localflavor. Spying on django_tests, these seem to be failing silently anyway; this is just making the "Error while importing" exception handler actually get called, where before they were silent. I'm not sure if there's another bug in there that this is exposing.

(Watch out: there's both eg. regressiontests.humanize and django.contrib.humanize for a few of those tests, and usually one of them works and the other doesn't.)

Attachments (2)

raise-exception-on-app-load.diff (604 bytes) - added by Glenn Maynard 11 years ago.
t10706.diff (6.0 KB) - added by Dennis Kaarsemaker 11 years ago.
Make the error message clearer

Download all attachments as: .zip

Change History (17)

Changed 11 years ago by Glenn Maynard

comment:1 Changed 11 years ago by Glenn Maynard

Has patch: unset

This causes other problems on app load later on. I'm not sure what the right fix is here; I'll look further when I have more time.

comment:2 Changed 11 years ago by nwelch

Django tries to import your app, and catches all ImportErrors when trying to do so, reporting them to the user as the app missing. So basically if you're trying to diagnose an unrelated ImportError in your project, it is incredibly hard to track it down without hacking django apart or some other contortion. I'm amazed so few people have reported a problem with this; it's very easy to run into when trying to use external Python libraries and whatnot.

I found the offending code in core/management/ in AppCommand.handle(). I think perhaps the component should be changed to core framework, although I'm a Django newbie so I'm not sure.

Changed 11 years ago by Dennis Kaarsemaker

Attachment: t10706.diff added

Make the error message clearer

comment:3 Changed 11 years ago by Dennis Kaarsemaker

Component: Database layer (models, ORM)
Has patch: set

The easiest and least intrusive solution is to make the error message say something about both possible causes of app load failure. I've been bitten by this quite a few times before and closed a bunch of duplicates of this ticket yesterday as well. Attached patch changes the error message and the testcases that explicitely test for this message.

comment:4 Changed 11 years ago by Glenn Maynard

That fixes the message being misleading, at least, but errors that say "it caused an error" are the worst sort. It really needs to say what the actual error is, and show a backtrace.

comment:5 Changed 11 years ago by anibal

OMG :(

my code was:
..auth.modls import User #note the missing 'e'

.. and I lost 1 hour, lesson learned: fix my ide | pylint allways

comment:6 Changed 11 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

comment:7 Changed 11 years ago by martin maney

Patch needs improvement: set

This bug may have the same root cause as #11696, or be a separate case where r10088 needlessly replaced a working __import__ with a call to import_module that conflates a missing with one that has a compiliation error that needs to be reported as such.

comment:8 Changed 11 years ago by cwb

This is easy to bump into and very puzzling (though with the beneficial side-effect that I properly dug into sys.path, relative imports and the like). For me the fix (to didn't work with admindocs enabled (not just the tests) because that doesn't have a models module. Still helpful though -- got me out of my pickle.

comment:9 Changed 11 years ago by xiongchiamiov

Cc: xiong.chiamiov@… added

comment:10 Changed 11 years ago by lawgon <lawgon@…>

any fix for this should also fix #11494 which has been closed as a duplicate of this. The same error that is reported here will also cause syncdb to fail silently, neither creating an error message nor adding the new model

comment:11 Changed 10 years ago by vincenth

Related to #7198

comment:12 Changed 10 years ago by santa4nt

I ran across this unhelpful error message as well. At the very least the --traceback option should display the traceback when requested. Executing ./ sql --traceback <app> doesn't say what it advertises.

comment:13 Changed 10 years ago by Annatar

I was a victim to this error message too. I reached desperation trying to figure out where did I do wrong in my file or in the folder where the new app resided. But it seems that there wasn't anything wrong with or the app folder in that respect. it was just a typing error within the file of the app.

it was something like:

from django.db import models
from django.contrib.auth.models import User
from Projectname.another_app.models import SomeModel #for foreign key usage

...and the new models defined here...

And instead of Projectname I should have typed ProjectName (silly error, but gave me quite a headache, because it's obscure)
Now I'm puzzled why Django only throws the correct errors only from the model classes and not from the starting declarations too. An error there tells me that the whole app is missing from, which is not true.

comment:14 Changed 9 years ago by Chris Beaven

Severity: Normal
Type: Bug

comment:15 Changed 9 years ago by Claude Paroz

Easy pickings: unset
Resolution: fixed
Status: newclosed
UI/UX: unset

I'm quite sure this should be fixed by now. Import errors in app are not swallowed any more. Reopen with precise instructions about how to reproduce if you can.

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