Search code examples
pythondjangocelerycelery-task

Why am I getting name 'shared_task' is not defined error with Celery


I am learning celery by following this tutorial.

I created the tasks as he did and tried running the code. But I get the following error

NameError: name 'shared_task' is not defined

This is my code

import requests
from bs4 import BeautifulSoup
import json
from datetime import datetime
import lxml

@shared_task
def scrape():
    try:
        ...

This code is working for him but I get the above mentioned error. Not sure what I am doing wrong. Please help me

Thanks in advance


Solution

  • You need to import the celery shared_task decorator as

    from celery import shared_task
    
    
    @shared_task
    def scrape():
        ...
        # your code