AttributeError:当docker-compose up时,'module'对象没有属性'celery'

时间:2018-08-01 09:46:33

标签: django python-2.7 redis docker-compose celery

我正在尝试在Docker上启动项目。在docker-compose up控制台发出后:

celery_redis_1  | Traceback (most recent call last):
celery_redis_1  |   File "/usr/local/bin/celery", line 11, in <module>
celery_redis_1  |     sys.exit(main())
celery_redis_1  |   File "/usr/local/lib/python2.7/site-packages/celery/__main__.py", line 16, in main
celery_redis_1  |     _main()
celery_redis_1  |   File "/usr/local/lib/python2.7/site-packages/celery/bin/celery.py", line 322, in main
celery_redis_1  |     cmd.execute_from_commandline(argv)
celery_redis_1  |   File "/usr/local/lib/python2.7/site-packages/celery/bin/celery.py", line 496, in execute_from_commandline
celery_redis_1  |     super(CeleryCommand, self).execute_from_commandline(argv)))
celery_redis_1  |   File "/usr/local/lib/python2.7/site-packages/celery/bin/base.py", line 273, in execute_from_commandline
celery_redis_1  |     argv = self.setup_app_from_commandline(argv)
celery_redis_1  |   File "/usr/local/lib/python2.7/site-packages/celery/bin/base.py", line 479, in setup_app_from_commandline
celery_redis_1  |     self.app = self.find_app(app)
celery_redis_1  |   File "/usr/local/lib/python2.7/site-packages/celery/bin/base.py", line 501, in find_app
celery_redis_1  |     return find_app(app, symbol_by_name=self.symbol_by_name)
celery_redis_1  |   File "/usr/local/lib/python2.7/site-packages/celery/app/utils.py", line 370, in find_app
celery_redis_1  |     found = sym.celery
celery_redis_1  | AttributeError: 'module' object has no attribute 'celery'

有人知道为什么找不到'celery'属性吗?谢谢你的帮助。 我的操作系统是macOS High Sierra 10.13.6

我的task.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import youtube_dl
import requests
import json
from django.conf import settings
from django.core.mail import send_mail
from celery import shared_task
from youtube_dl import YoutubeDL

@shared_task(name='download')
def download(url, email):
...

我的docker-compose.yml:

version: "3"
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    links:
      - celery_redis
      - redis

  redis:
    image: redis
    command: redis-server
    ports:
      - "6379:6379"

  celery_redis:
    image: celery_redis
    command: celery worker --app=email_mp3_converter.tasks
    links:
      - redis

我的Dockerfile:

FROM python:2.7.10
ENV DJANGO_SETTINGS_MODULE email_mp3_converter.settings
RUN pip install --upgrade pip
ADD requirements.txt /email_mp3_converter/requirements.txt
RUN pip install -r /email_mp3_converter/requirements.txt
ADD . /email_mp3_converter
CMD python /email_mp3_converter/manage.py runserver 0.0.0.0:8080

我的要求。txt:

Django==1.11
requests==2.19.1
youtube_dl==2018.6.19
django-redis==4.9.0
django-registration==2.4.1
djangorestframework==3.8.2
youtube-dl==2018.6.19
celery==4.2.0
django_celery_beat

芹菜文件:

from __future__ import absolute_import

import os

from celery import Celery


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

app = Celery('email_mp3_converter', broker='amqp://guest@localhost//')

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

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

我做错了什么以及如何解决?谢谢您的提前帮助

1 个答案:

答案 0 :(得分:0)

我遇到了完全相同的问题。 当我执行

    docker-compose run web bash
    # Inside
    cd /code/ && celery -A app worker -l info

比它工作得还不错,但不能与docker-compose up一起使用

在docker-compose.yml文件中似乎我的音量设置不正确。 我将其更改为

 command: bash -c "cd /code/ && celery -A app beat -l info"
    volumes:
      - ./:/code

,错误消失了。我认为此问题的解决方案是检查您在Dockerfile中设置的WORKDIR以及在docker-compose.yml文件中用于芹菜映像的卷。很难给出“在所有情况下都能工作”的解决方案。

请注意,您首先应通过登录docker shell来检查celery是否完全运行。

希望可以缩小问题范围。