[Fixed]-Celery autodiscover_tasks not working for all Django 1.7 apps

23👍

This is discussed in a number of Celery issues, such as #2596 and #2597.

If you are using Celery 3.x, the fix is to use:

from django.apps import apps
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])

As mentioned in #3341, if you are using Celery 4.x (soon to be released) you can use:

app.autodiscover_tasks()
👤Doddie

3👍

I just had this problem because of a misconfigured virtual environment.

If an installed app has a dependency missing from the virtual environment in which you’re running celery, then the installed app’s tasks will not be auto discovered. This hit me as I was moving from running my web server and celery on the same machine to a distributed solution. A bad build resulted in different environment files on different nodes.

I added the dependencies that were missing then restarted the celery service.

0👍

I had to add this to the module where my celery app was defined:

from __future__ import absolute_import

0👍

My problem was the wrong path to the celery.py in the celery command.
the command should be something like this:

celery worker -A <project_name.celery_app_module> -l info

where celery celery_app_module is like this:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# Set default Django settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project_name>.settings')

app = Celery('<project_name>', include=['<app_name>.tasks'])
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

Leave a comment