Prometheus Prometheus - 2 months ago 19
Python Question

Django Apps aren't loaded yet Celery Tasks

What is giving the error below? I'm unsure if this is a problem with an app I have installed or one of mine. The exception below is generated only when running Celery i.e.

celery -A demo.apps.wall.tasks worker
, runserver does not generate any errors. Which app ios the issue?

Traceback (most recent call last):
File "/Users/user/Documents/workspace/demo-api/env/bin/celery", line 11, in <module>
sys.exit(main())
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/__main__.py", line 30, in main
main()
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/celery.py", line 770, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/base.py", line 309, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
self.app = self.find_app(app)
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/bin/base.py", line 489, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/app/utils.py", line 238, in find_app
sym = imp(app)
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/celery/utils/imports.py", line 101, in import_from_cwd
return imp(module, package=package)
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/user/Documents/workspace/demo-api/demo/apps/Walls/tasks.py", line 14, in <module>
from demo.apps.Walls.redis_models import WallSchedule, WallBroadcast, UserWalls
File "/Users/user/Documents/workspace/demo-api/demo/apps/Walls/redis_models.py", line 12, in <module>
from demo.apps.memberships.models import Membership
File "/Users/user/Documents/workspace/demo-api/demo/apps/memberships/models.py", line 4, in <module>
from django.contrib.contenttypes.models import ContentType
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 161, in <module>
class ContentType(models.Model):
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/Users/user/Documents/workspace/demo-api/env/lib/python3.5/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.


Running
manage.py check
is ok.

Answer

Ok - after posting your celery app files I compared to what I have and tried running. Think I found your issue - it looks like you're calling tasks.py in celery -A demo.apps.wall.tasks.

If your wall module contains celery.py, and tasks.py - you should call celery -A demo.apps.wall.

Here's the directory structure I have, and the celery command I run:

django_project
- an_app
- celery_tasks
   - init.py
   - celery_app.py (celery.py in your case)
   - tasks.py

The command I run: celery worker -A celery_tasks from the django_project directory.

Comments