Question

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
Was it helpful?

Solution

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

OTHER TIPS

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

For those who are still encountering this error even after renaming your celery.py file inside your project directory, just delete the celery.pyc file as well.

If you have installed Celery in virtual environment, make sure you have the same python interpreter selected corresponding to that virtual environment.

In VS Code, you can select the interpreter from here.

View - Command Palette - Python: Select Interpreter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top