0
0
DjangoHow-ToBeginner · 4 min read

How to Install Celery in Django: Step-by-Step Guide

To install Celery in a Django project, run pip install celery and then create a celery.py file in your Django project to configure it. Finally, update your __init__.py to load Celery when Django starts.
📐

Syntax

Installing Celery requires the Python package and basic setup files in your Django project.

  • pip install celery: Installs the Celery package.
  • celery.py: Configures Celery with your Django settings.
  • __init__.py: Ensures Celery starts with Django.
bash
pip install celery
💻

Example

This example shows how to install Celery, configure it in a Django project, and create a simple task.

python
1. Install Celery:

pip install celery

2. Create a file <code>celery.py</code> in your Django project folder (same level as <code>settings.py</code>):

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

3. In <code>__init__.py</code> of the same folder, add:

from .celery import app as celery_app

__all__ = ('celery_app',)

4. Create a task in any app, e.g., in <code>tasks.py</code>:

from celery import shared_task

@shared_task
def add(x, y):
    return x + y

5. Run Celery worker in terminal:

celery -A myproject worker --loglevel=info
Output
-------------- celery@hostname v5.x.x (singularity) --- ***** ----- --- * *** * -- Linux-... --- * - * * -- Python 3.x.x --- *** ----- -------------- [configurations loaded] [tasks registered] [worker ready]
⚠️

Common Pitfalls

Common mistakes when installing Celery in Django include:

  • Not setting the DJANGO_SETTINGS_MODULE environment variable before starting Celery.
  • Forgetting to import Celery app in __init__.py, so Celery does not start with Django.
  • Not running a message broker like RabbitMQ or Redis, which Celery needs to work.
  • Using old Celery syntax or not matching Celery version with Django compatibility.
python
Wrong way:

# Missing import in __init__.py
# Celery app not loaded, tasks won't run

Right way:

# __init__.py
from .celery import app as celery_app

__all__ = ('celery_app',)
📊

Quick Reference

StepCommand / FilePurpose
1pip install celeryInstall Celery package
2celery.pyConfigure Celery with Django settings
3__init__.pyImport Celery app to start with Django
4tasks.pyDefine asynchronous tasks
5celery -A myproject worker --loglevel=infoRun Celery worker process

Key Takeaways

Install Celery using pip and configure it in a celery.py file inside your Django project.
Always import the Celery app in your __init__.py to ensure it loads with Django.
Run a message broker like Redis or RabbitMQ before starting Celery workers.
Use @shared_task decorator to define tasks that Celery can run asynchronously.
Check environment variables and Celery version compatibility to avoid common errors.