Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import error in celery

Tags:

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
like image 606
amit_183 Avatar asked Oct 30 '13 04:10

amit_183


2 Answers

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

like image 103
Jaredp37 Avatar answered Oct 18 '22 09:10

Jaredp37


I had this issue together with pycharm and unittests, and want to add an additional answer. I have tested it with python 3 but assume the same happens if you use from __future__ import absolute_import with earlier versions.

The root cause is described in the pycharm bug report https://youtrack.jetbrains.com/issue/PY-15889

Summary: If you run unittests in pycharm, it always adds the folder containing the test script to the beginning of sys.path. If your celery.py is in the same path, python will try to load it first.

Instead of changing file names I have resolved this by removing this folder from sys.path before importing celery.py in my ..._Test.py test scripts.

# If running in pycharm, pycharm always adds the current path to the beginning (!) of sys.path
# I have not found a setting to fix this - so I just remove the path again
# This block can be removed once https://youtrack.jetbrains.com/issue/PY-15889 if fixed
import sys
import os
sys.path.remove(os.path.dirname(__file__)) 

import celery
like image 42
ootwch Avatar answered Oct 18 '22 10:10

ootwch