28

I try to start a Celery worker server from a command line:

celery -A tasks worker --loglevel=info

The code in tasks.py:

import os
os.environ[ 'DJANGO_SETTINGS_MODULE' ] = "proj.settings"

from celery import task

@task()
def add_photos_task( lad_id ):
...

I get the next error:

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 8, in <module>
    load_entry_point('celery==3.0.12', 'console_scripts', 'celery')()
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/__main__.py", line 14, in main
    main()
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/celery.py", line 946, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/celery.py", line 890, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/base.py", line 177, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/base.py", line 295, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python2.7/site-packages/celery-3.0.12-py2.7.egg/celery/bin/base.py", line 313, in find_app
    return sym.celery
AttributeError: 'module' object has no attribute 'celery'

Does anybody know why the 'celery' attribute cannot be found? Thank you for help.

The operating system is Linux Debian 5.

Edit. May be the clue. Could anyone explain me the next comment to a function (why we must be sure that it finds modules in the current directory)?

# from celery/utils/imports.py
def import_from_cwd(module, imp=None, package=None):
    """Import module, but make sure it finds modules
    located in the current directory.

    Modules located in the current directory has
    precedence over modules located in `sys.path`.
    """
    if imp is None:
        imp = importlib.import_module
    with cwd_in_path():
        return imp(module, package=package)
sergzach
  • 6,578
  • 7
  • 46
  • 84

6 Answers6

31

I forgot to create a celery object in tasks.py:

from celery import Celery
from celery import task  

celery = Celery('tasks', broker='amqp://guest@localhost//') #!

import os

os.environ[ 'DJANGO_SETTINGS_MODULE' ] = "proj.settings"

@task()
def add_photos_task( lad_id ):
...

After that we could normally start tasks:

celery -A tasks worker --loglevel=info
sergzach
  • 6,578
  • 7
  • 46
  • 84
17

For anyone who is getting the same error message for an apparently different reason, note that if any of the imports in your initialization file fail, your app will raise this totally ambiguous AttributeError rather than the exception that initially caused it.

kellanburket
  • 12,250
  • 3
  • 46
  • 73
  • Thanks for the clue. I get this when I've accidentally got imports that become circular, so that is something to watch for. – Terri Simon Jun 25 '19 at 17:51
  • thank you! I was using an undeclared variable inside `if __name__ == '__main__':` because I missspelled it. I was getting crazy looking at my project directories because the problem was not there! neither in celeryconfig! – Shil Nevado Apr 28 '20 at 12:28
12

Celery uses celery file for storing configuration of your app, you can't just give a python file with tasks and start celery. You should define celery file ( for Celery>3.0; previously it was celeryconfig.py)..

celeryd --app app.celery -l info

This example how to start celery with config file at app/celery.py

Here is example of celery file: https://github.com/Kami/libcloud-sandbox/blob/master/celeryconfig.py

user
  • 5,335
  • 7
  • 47
  • 63
Rustem
  • 2,884
  • 1
  • 17
  • 32
  • Thank you. But I do this on Windows, it works normally. I have not a special configuration file. Do I understand the Celery docs correctly (could you take a look please?): http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#running-the-celery-worker-server – sergzach Nov 26 '12 at 16:26
  • Where you store you broker settings and etc. – Rustem Nov 26 '12 at 16:27
  • Yes, if you change it according to my. – sergzach Nov 26 '12 at 18:00
  • What did you actually forgot to do? I have similar problem (see http://stackoverflow.com/questions/17007101/starting-celery-with-supervisord-attributeerror-module-object-has-no-attribu) – kanitw Jun 09 '13 at 06:38
3

My problem was that I put the celery variable inside a main function:

if __name__ == '__main__':  # Remove this row
    app = Flask(__name__)
    celery = make_celery(app) 

when it should be put outside.

Philip
  • 3,135
  • 2
  • 29
  • 43
2

When you run celery -A tasks worker --loglevel=info, your celery app should be exposed in the module tasks. It shouldn't be wrapped in a function or an if statements that.

If you make_celery in another file, you should import the celery app in to your the file you are passing to celery.

Chuma Umenze
  • 933
  • 12
  • 18
1

Try start celery:

celeryd --config=my_app.my_config --loglevel=INFO --purge -Q my_queue

There is next script in my tasks.py:

@task(name="my_queue", routing_key="my_queue")
def add_photos_task( lad_id ):

There is next script in my_config.py:

CELERY_IMPORTS = \
(
    "my_app.tasks",
)
CELERY_ROUTES = \
{
    "my_queue":
    {
        "queue": "my_queue"
    },
}
CELERY_QUEUES = \
{
    "my_queue":
    {
        "exchange": "my_app",
        "exchange_type": "direct",
        "binding_key": "my_queue"
    },
}
celery = Celery(broker='amqp://guest@localhost//')
Evgenii
  • 3,283
  • 3
  • 27
  • 32