Anee
Anee

Reputation: 35

Unable to properly import celery

I am trying to run a Celery demo. It gives a import error.

     File "/home/anee/anaconda2/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/home/anee/anaconda2/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/anee/Documents/off-ds/t3/run_tasks.py", line 2, in <module>
    from t3tasks import longtime_add
  File "t3/t3tasks.py", line 3, in <module>
    from t3.celery import app
  File "t3/celery.py", line 3, in <module>
    from celery import Celery
ImportError: cannot import name Celery

Here is my directory structure

t3 
    __init__.py
    celery.py
    run_tasks.py
    t3tasks.py

Celery file

from celery import Celery

app = Celery('t3',
             broker='amqp://',
             backend='amqp://',
             include=['t3.t3tasks'])

Here is t3tasks.py file

from t3.celery import app
import time

@app.task
def longtime_add(x, y):
    print('long time task begins')
    # sleep 5 seconds
    time.sleep(5)
    print('long time task finished')
    return x + y

Here is run_tasks.py

from t3tasks import longtime_add
import time

if __name__ == '__main__':
    result = longtime_add.delay(1,2)
    print('Task result: ', result.result)

After starting celery

celery -A t3 worker --loglevel=info

I am running the tasks like this

python -m t3.run_tasks

Upvotes: 0

Views: 617

Answers (1)

Yoichi Nakayama
Yoichi Nakayama

Reputation: 774

Rename the file t3/celery.py to another name.

from celery import Celery

in your t3/celery.py tries to import Celery from t3/celery.py itself (instead of celery module in the library).

Upvotes: 1

Related Questions