首页 > 解决方案 > python celery: ImportError: cannot import name from 'celery_worker' (/home/simha/app/src/celery_worker.py)

问题描述

我有以下文件夹

app
|--venv
|--src
   |--celery_worker.py
   |--test.py

celery_worker.py

from celery import Celery

celery = Celery("tasks", broker="redis://redis:6379/0")

@celery.task(name="test")
def test():
    print("test print")

def test2():
    print("test2 print")


test.py

def hare():
    print("testing")

现在在src文件夹中我打开了一个 python shell 并检查

>>> from test import hare
>>> hare()
testing

然而

>>> from celery_worker import test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'test' from 'celery_worker' (/home/simha/app/src/celery_worker.py)

并且

>>> from celery_worker import test2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'test2' from 'celery_worker' (/home/simha/app/src/celery_worker.py)

标签: celery

解决方案


推荐阅读