Object "Celery" has no attribute "application" when using Python 3

I am studying herring tutorial. They are using Python2 and I am trying to implement the same with python3.

I have 2 files:

celery_proj.py :

from celery import Celery

app = Celery(
    'proj', broker='amqp://', backend='amqp://', include=['proj.tasks'])

app.conf.update(Celery_TAST_RESULT_EXPIRES=3600,)

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

      

and tasks.py :

from celery_proj 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)

      

When I try to run celery -A proj worker -l info

I get:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/celery/app/utils.py", line 235, in find_app
    found = sym.app
AttributeError: 'module' object has no attribute 'app'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.4/dist-packages/celery/__main__.py", line 30, in main
    main()
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 309, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 489, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/local/lib/python3.4/dist-packages/celery/app/utils.py", line 240, in find_app
    found = sym.celery
AttributeError: 'module' object has no attribute 'celery'

      

What am I doing wrong and how do I fix it?

+4


source to share


4 answers


If you are using Python 3, you can use absolute imports via: from __future__ import absolute_import

Here's an example celery.py

from a recent Python 3 application of mine, Django 1.7:



from __future__ import absolute_import

import os
import django

from celery import Celery
from django.conf import settings


# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_app.settings')
django.setup()

app = Celery('my_app')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

      

+3


source


When you run Celery with celery -A proj worker

AttributeError: 'module' object has no attribute 'app'
...
AttributeError: 'module' object has no attribute 'celery'

      

to tell you what he's trying to find:

a) instance app



b) module proj.celery

So you can add from celery_proj import app

for from celery_proj import app

to __init__.py

or rename yours celery_proj.py

to celery.py

.

Or you can start Celery as celery -A proj.celery_proj worker

I found the answer here, as no answer here on stackoverflow helped me.

+3


source


I played around and realized one interesting thing. When we run celery -A proj worker -l info

we basically run the proj

folder. I believe that when you launch the proj

folder, celery looks for the celery.py

file there. This was fine in python2, since we used absolute imports and could write, from proj.celery import...

but in python3 this is not possible. We need to write from celery import...

and this will celery

throw an error, as it is a module, so we need to rename celery.py to something else. When we do this, we can no longer run proj

. Maybe I'm wrong, but at least I made it work ...

What you need to do is open the proj

directory and run tasks.py from there, only then will you be able to use from celery_proj import app

and save celery_proj

.

Please post if I'm wrong and add your solutions.

+2


source


I had the same problem running celery and django app in a dock container. It looks like the celery worker command is looking in the current directory for a celery application. When the command was run from any arbitrary directory, this attribute was not found. But when it runs from the django app directory (where it can find the app / modules), the celery worker works as expected.

0


source







All Articles