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
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:
If you call celery from the root folder, you should be able to type:
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
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 yourcelery.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 importingcelery.py
in my..._Test.py
test scripts.For those who are still encountering this error even after renaming your
celery.py
file inside your project directory, just delete thecelery.pyc
file as well.