I can't use celery tasks

I have the following folder:

 ├── __init__.py
 ├── celery.py
 ├── tasks.py
 ├── foo_launcher.py

Firstly, i have to launch celery worker.

celery -A proj worker -l info

celery.py code:

from __future__ import absolute_import, unicode_literals
from celery import Celery

app = Celery('project', broker='amqp://guest@localhost//', include=['project.tasks'])

if __name__ == '__main__':

Celery worker works perfectly but when i going to launch foo_launcher.py, i can't use the tasks because foo_launcher.py depends on tasks.py and tasks.py depends on celery.py. Here is the problem. I need to import celery.py file not celery library.

So, this is tasks.py

from  .celery import app #Here Import celery.py not celery library

def add(x, y):
   return x + y

and here foo_launcher.py

from tasks import add

result = add.apply_async((2,2), queue='lopri',coutdown=10)

All makes sense aparently but when i try to launch foo_launcher.py:

python foo_launcher.py

It launchs to me the following error:

Traceback (most recent call last):
    File ".\foo_launcher.py", line 1, in <module>
        from tasks import add
    File "C:\Users\beca4\Desktop\proj\tasks.py", line 1, in <module>     
        from  .celery import app                                                                                           
ImportError: attempted relative import with no known parent package   

I don't know what can i do. Any ideas?

1 answer

  • answered 2018-11-08 08:15 liaofeng

    celery.py has the same name with celery module.

    you'd better change celery.py's file name, for example, mq.py