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:

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.)

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.

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.

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.

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.

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

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.

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.

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.