djangoproject.com | python.org | nginx.org
version seven.
  http://demongin.org
demongin.org - When <tt>manage.py</tt> Tools Refuse to Find Your New App

When manage.py Tools Refuse to Find Your New App

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


Monday, July 30, 2012 | Django

django.core.exceptions.ImproperlyConfigured: App with label whatever could not be found

So, I'm doing some rapid-fire prototyping for this after hours piece of skunk work--who knows why--and all of a sudden I find that I am stuck: I am running manage.py syncdb to add a new app's models to my database and it's not doing anything and I have no idea why.

Basic Troubleshooting

When the manage.py tools fail to find your app, it's usually because you typed something incorrectly: either you've got a typo in settings.INSTALLED_APPS or, perhaps, you tab-completed your manage.py command and included a trailing slash. Sometimes you created an app directory by hand--not a best practice--and failed to create models.py and init.py in your app directory. This will also cause the manage.py tools to fail.

Once you rule out PEBKAC in either of those two forms--the typo or the failure to create mandatory files--you may find yourself (as I just did) with a serious head-scratcher on your hands.

When this happens, your next troubleshooting step should be to check permissions.

It is commonly known that when you run manage.py startapp in modern versions of Django, you get the files models.py and init.py for free. What is less commonly known is that both manage.py syncdb and manage.py test appname use the compiled versions of these files to verify that they exist (and then do things with them).

And so, if you executed manage.py runserver as root, you created .pyc files that belong to root and, if you run either syncdb or test as yourself (i.e. not as root), they will fail:

toconnell@li66-186:~/project$ ./manage.py test whatever
Traceback (most recent call last):
  File "./manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 362, in execute_manager
    utility.execute()
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 303, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 195, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 222, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/pymodules/python2.6/django/core/management/commands/test.py", line 23, in handle
    failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive)
  File "/usr/lib/pymodules/python2.6/django/test/simple.py", line 178, in run_tests
    app = get_app(label)
  File "/usr/lib/pymodules/python2.6/django/db/models/loading.py", line 125, in get_app
    raise ImproperlyConfigured, "App with label %s could not be found" % app_label
django.core.exceptions.ImproperlyConfigured: App with label whatever could not be found
</module>

Fix the File System

Here's what to do next:
  1. Check permissions:
    toconnell@li66-186:~/project/whatever$ ls -l
    total 24
    -rw-r--r-- 1 toconnell toconnell    0 2012-07-30 20:09 __init__.py
    -rw-r--r-- 1 root      root       202 2012-07-30 20:09 __init__.pyc
    -rw-r--r-- 1 toconnell toconnell 1495 2012-07-30 20:41 models.py
    -rw-r--r-- 1 root      root      2182 2012-07-30 20:41 models.pyc
    -rw-r--r-- 1 toconnell toconnell  514 2012-07-30 20:09 tests.py
    -rw-r--r-- 1 toconnell toconnell  591 2012-07-30 20:27 views.py
    -rw-r--r-- 1 root      root      1000 2012-07-30 20:27 views.pyc
    
  2. Remove the .pyc files owned by root:
    toconnell@li66-186:~/project/whatever$ rm *.pyc
    rm: remove write-protected regular file `__init__.pyc'? y
    rm: remove write-protected regular file `models.pyc'? y
    rm: remove write-protected regular file `views.pyc'? y
    
  3. And try again:
    toconnell@li66-186:~/project$ ./manage.py syncdb
    Creating table whatever_whateverthing
    toconnell@li66-186:~/project$ ./manage.py test whatever
    Creating test database...
    Creating table auth_permission
    Creating table auth_group
    Creating table auth_user
    Creating table auth_message
    Creating table django_content_type
    Creating table django_session
    Creating table django_site
    Creating table django_admin_log
    Creating table whatever_whateverthing
    Installing index for auth.Permission model
    Installing index for auth.Message model
    Installing index for admin.LogEntry model
    ..
    ----------------------------------------------------------------------
    Ran 2 tests in 0.029s
    
    OK
    Destroying test database...
    

Now pull it together and get back out there, rook.