I am trying to use celery with django with redis as the broker. One of my app name is user and its models are imported by other apps. When I run the django server it starts without any error and everything works fine.
But when I try to start a celery worker with the command "celery -A project worker -l INFO", I get the following error:
Traceback (most recent call last):
File "/usr/local/bin/celery", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 30, in main
main()
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 793, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 311, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 785, in handle_argv
return self.execute(command, argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 717, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "/usr/local/lib/python2.7/dist-packages/celery/bin/worker.py", line 179, in run_from_argv
return self(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 274, in __call__
ret = self.run(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/worker.py", line 212, in run
state_db=self.node_format(state_db, hostname), **kwargs
File "/usr/local/lib/python2.7/dist-packages/celery/worker/__init__.py", line 95, in __init__
self.app.loader.init_worker()
File "/usr/local/lib/python2.7/dist-packages/celery/loaders/base.py", line 128, in init_worker
self.import_default_modules()
File "/usr/local/lib/python2.7/dist-packages/celery/loaders/base.py", line 116, in import_default_modules
signals.import_modules.send(sender=self.app)
File "/usr/local/lib/python2.7/dist-packages/celery/utils/dispatch/signal.py", line 166, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/local/lib/python2.7/dist-packages/celery/fixups/django.py", line 73, in on_import_modules
self.worker_fixup.validate_models()
File "/usr/local/lib/python2.7/dist-packages/celery/fixups/django.py", line 158, in validate_models
django_setup()
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/path_to_project/project/app1/models.py", line 2, in <module>
from app1.models import *
File "/path_to_project/project/project/models.py", line 9, in <module>
from user.models import *
ImportError: No module named models
The thing to be noted here is that it says No module named models and not No module named user.models. This can mean that there is some conflict with other file although I have double checked and there is no user.py in either project directory or app1 directory. If I rename app "user" to "user2" the worker starts. Also to be noted is the fact that celery was starting fine sometime back. Since then the code has changed but no app has been added.
My project has the following structure (showing only relevant parts)
- project
- project
- celery.py
- models.py
- user
- models.py
- app1
- models.py
- tasks.py
and celery.py is
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app = Celery('project')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
CELERY config is
BROKER_URL = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
Thanks