Opened 15 years ago

Closed 12 years ago

#10706 closed Bug (fixed)

Incorrect error from manage.py sql when app fails to load

Reported by: Glenn Maynard Owned by: nobody
Component: Core (Management commands) Version: dev
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

Description

./manage.py 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/loading.py load_app to print the exception, the problem becomes obvious:

./manage.py 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 "./runtests.py", line 134, in django_tests
    mod = load_app(model_label)
  File "/home/glenn/django/django/db/models/loading.py", line 74, in load_app
    models = import_module('.models', app_name)
  File "/home/glenn/django/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named models

for humanize, syndication, sitemaps, databrowse, admindocs and localflavor. Spying on runtests.py 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 15 years ago.
t10706.diff (6.0 KB ) - added by Dennis Kaarsemaker 15 years ago.
Make the error message clearer

Download all attachments as: .zip

Change History (17)

by Glenn Maynard, 15 years ago

comment:1 by Glenn Maynard, 15 years ago

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 by nwelch, 15 years ago

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/base.py 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.

by Dennis Kaarsemaker, 15 years ago

Attachment: t10706.diff added

Make the error message clearer

comment:3 by Dennis Kaarsemaker, 15 years ago

Component: Database layer (models, ORM)django-admin.py
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 by Glenn Maynard, 15 years ago

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 by anibal, 15 years ago

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 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:7 by martin maney, 15 years ago

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 models.py with one that has a compiliation error that needs to be reported as such.

comment:8 by cwb, 14 years ago

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 loading.py) 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 by xiongchiamiov, 14 years ago

Cc: xiong.chiamiov@… added

comment:10 by lawgon <lawgon@…>, 14 years ago

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 by vincenth, 14 years ago

Related to #7198

comment:12 by santa4nt, 14 years ago

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

comment:13 by Annatar, 14 years ago

I was a victim to this error message too. I reached desperation trying to figure out where did I do wrong in my settings.py file or in the folder where the new app resided. But it seems that there wasn't anything wrong with settings.py or the app folder in that respect. it was just a typing error within the modules.py 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 settings.py, which is not true.

comment:14 by Chris Beaven, 13 years ago

Severity: Normal
Type: Bug

comment:15 by Claude Paroz, 12 years ago

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

I'm quite sure this should be fixed by now. Import errors in app models.py 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