I’m trying to set up a background task using celery and rabbitmq on django but I’m getting an error saying that my project has no attribute celery. I’m using PyCharm and installed celery through that.
I’m new to celery but I’ve read a lot of articles similar to this issue (AttributeError: module ‘module’ has no attribute ‘celery’ this one seems the closest but not sure it’s quite the same)
Project structure:
JavaScript
x
8
1
project_name
2
├──project_name
3
| ├── settings.py
4
├──app_name1
5
| └──celery.py
6
├──app_name2
7
| └──tasks.py
8
I run the following command:
JavaScript
1
2
1
celery -A project_name worker -l info --pool=solo
2
But I get the following error:
JavaScript
1
4
1
Error: Invalid value for "-A" / "--app":
2
Unable to load celery application.
3
Module 'project_name' has no attribute 'celery'
4
celery.py file:
JavaScript
1
14
14
1
from __future__ import absolute_import, unicode_literals
2
3
import os
4
5
from celery import Celery
6
7
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
8
9
app = Celery('project_name')
10
11
app.config_from_object('django.config:settings', namespace='CELERY')
12
13
app.autodiscover_tasks()
14
tasks.py file:
JavaScript
1
8
1
from __future__ import absolute_import, unicode_literals
2
3
from celery import shared_task
4
5
@shared_task
6
def add(x, y):
7
return x + y
8
Advertisement
Answer
Just for the record: Put celery.py
file into the main project file, not inside the app.