无法正确进口芹菜

时间:2018-03-17 08:48:00

标签: python rabbitmq celery

我正在尝试运行Celery演示。它会导致导入错误。

     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

这是我的目录结构

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

芹菜文件

from celery import Celery

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

这是t3tasks.py文件

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

这是run_tasks.py

from t3tasks import longtime_add
import time

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

开始芹菜后

celery -A t3 worker --loglevel=info

我正在运行这样的任务

python -m t3.run_tasks

1 个答案:

答案 0 :(得分:0)

将文件t3 / celery.py重命名为其他名称。

this._nameService
    .getNames()
    .subscribe(res => { 
                          this.nameLists = res;
                          console.log(JSON.stringify(this.nameLists));
                      });
t3 / celery.py中的

尝试从t3 / celery.py本身导入Celery(而不是库中的celery模块)。