import error in celery

amit_183 picture amit_183 · Oct 30, 2013 · Viewed 17.6k times · Source

this is the code which i am running:

from __future__ import absolute_import
from celery import Celery
celery1 = Celery('celery',broker='amqp://',backend='amqp://',include=['tasks'])

celery1.conf.update(
   CELERY_TASK_RESULT_EXPIRES=3600,
       )
if __name__ == '__main__':
 celery1.start()

when i execute the above code it gives me the following error:

   ImportError: cannot import name Celery

Answer

Jaredp37 picture Jaredp37 · Dec 12, 2013

I ran into this same error as well and renaming the file fixed it.

For anyone else encountering this, the reason WHY you get this issue, your local celery.py is getting imported instead of the actual celery package, hence the reason python can't find Celery(capital "C"), as in the class within the celery package, but it does find celery(lowercase "c"), which is your local celery.py that doesn't have the Celery class defined inside of it.

I just renamed my local celery.py to _celery.py, but any name other than celery.py should resolve the issue.

Edit: I also meant to mention that calling the celery daemon outside of the directory the celery.py file is located in will work as well. For testing purposes renaming the file will suffice, but you can still use celery.py as your local filename. For example, given the below folder structure:

+ root/  
   - proj/  
     * celery.py  
     * tasks.py

If you call celery from the root folder, you should be able to type:

celery worker --app=proj [optional args]

You can verify your tasks are present using the "-l info" optional log argument and viewing the [tasks] listing directly below the intro data.

More info is in the tutorial docs here: http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#about-the-app-argument