Search code examples
pythoncelerycelery-task

python celery invalid value for -A unable to load application


I have a following project directory:

azima:
    __init.py
    main.py
    tasks.py

task.py

from .main import app

@app.task
def add(x, y):
    return x + y

@app.task
def mul(x, y):
    return x * y

@app.task
def xsum(numbers):
    return sum(numbers)

main.py

from celery import Celery

app = Celery('azima', backend='redis://localhost:6379/0', broker='redis://localhost:6379/0', include=['azima.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
    result_expires=3600,
)

if __name__ == '__main__':
    app.start()

When I run a command to initialize celery workers, I get this error:

(azima_venv) brightseid@darkseid:~$ celery -A azima worker -l INFO
Usage: celery [OPTIONS] COMMAND [ARGS]...

Error: Invalid value for '-A' / '--app': 
Unable to load celery application.
Module 'azima' has no attribute 'celery'

But when I rename main.py to celery.py as it was earlier there's no issue.

What am I missing here?


Solution

  • There are two approaches

    1. import your app to azima/__init__.py
    from azima.main import app
        
    celery = app  # you can omit this line
    

    You can omit the last line, celery will recognize the celery app from the import. Then you call celery -A azima worker -l INFO

    1. Call you application like celery -A azima.main worker -l INFO