Celery 'module' object has no attribute 'app' when using Python 3

Celery 'module' object has no attribute 'app' when using Python 3

我正在学习 Celery 教程。他们正在使用 Python2 而我正在尝试使用 python3.

来实现相同的功能

我有 2 个文件:

celery_proj.py :

from celery import Celery

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

app.conf.update(Celery_TAST_RESULT_EXPIRES=3600,)

if __name__ == '__main__':
    app.start()

tasks.py

from celery_proj import app


@app.task
def add(x, y):
    return x + y


@app.task
def mul(x, y):
    return x * y


@app.task
def xsum(numbers):
    return sum(numbers)

当我尝试 运行 celery -A proj worker -l info 我得到:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/celery/app/utils.py", line 235, in find_app
    found = sym.app
AttributeError: 'module' object has no attribute 'app'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.4/dist-packages/celery/__main__.py", line 30, in main
    main()
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 309, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python3.4/dist-packages/celery/bin/base.py", line 489, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/local/lib/python3.4/dist-packages/celery/app/utils.py", line 240, in find_app
    found = sym.celery
AttributeError: 'module' object has no attribute 'celery'

我做错了什么以及如何解决这个问题?

我一直在玩,并且了解了一件有趣的事情。 当我们 运行 celery -A proj worker -l info 我们基本上是 运行 proj 文件夹。我相信,当你 运行 proj 文件夹时,芹菜会在那里寻找 celery.py 文件。在 python2 中没问题,因为我们使用绝对导入并且可以编写 from proj.celery import ... 但在 python3 中没有这种可能性。我们需要写 from celery import ... ,这会导致错误,因为 celery 是一个模块,所以我们需要将 celery.py 重命名为其他名称。当我们这样做时,我们不能再 运行 proj 了。也许我错了,但至少我成功了...

你需要做的是打开 proj 目录和 运行 tasks.py 从那里,只有这样你才能使用 from celery_proj import app 并保留 celery_proj.

如有错误请留言补充解决方法

如果您正在使用 Python 3,您 可以 通过以下方式使用绝对导入:from __future__ import absolute_import

这是我最近 Python 3、Django 1.7 应用程序中的示例 celery.py

from __future__ import absolute_import

import os
import django

from celery import Celery
from django.conf import settings


# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_app.settings')
django.setup()

app = Celery('my_app')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

当你 运行 芹菜与 celery -A proj worker

AttributeError: 'module' object has no attribute 'app'
...
AttributeError: 'module' object has no attribute 'celery'

告诉你它试图找到:

a) app 实例

b) proj.celery 模块

因此您可以将 from celery_proj import app 添加到 __init__.py 或将 celery_proj.py 重命名为 celery.py

或者你可以 运行 芹菜作为 celery -A proj.celery_proj worker

我找到了答案 here,因为 Whosebug 上的答案对我没有帮助。

我在 运行 在 docker 容器中安装 celery 和 django 应用程序时遇到了同样的问题。看起来 celery worker 命令在 当前目录 中搜索 celery 应用程序。当命令是来自任意目录的 运行 时,会发生此属性未找到错误。但是当它来自 django 应用程序目录(它可以在其中找到 app/modules)的 运行 时,芹菜工人 运行s 如预期的那样。

当我为主项目目录和应用程序目录提供相同的名称时,我遇到了 docker 的奇怪情况。

更正卷中的路径解决了我的问题(-./app:/app)检查 docker 部分中的 celery 配置部分。

-app:
    ..
    app:
        __init__.py
        celery.py

init.py

from __future__ import absolute_import

from .celery import app as celery_app

__all__ = ['celery_app']

celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')

app = Celery('app')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

因此我必须在卷部分提供正确的路径

celery:
    build: .
    command: celery -A app worker -l info
    volumes:
     - ./app:/app
    depends_on:
     - db
     - redis