簡體   English   中英

模塊“module”沒有屬性“celery”

[英]Module 'module' has no attribute 'celery'

我需要在我的項目中使用 Celery,但是當我嘗試在我的項目中安裝時遇到了一些錯誤。 我已經閱讀了DjangoCelery 文檔,但我一直在犯同樣的錯誤。

我的樹項目:

  • 📂 src
    • 📄 __init__.py

    • 📄芹菜.py

    • 📂核心

      • 📄 __init__.py
    • 📂 project_auth

      • 📄 __init__.py
      • 📄serializer.py
      • 📄任務.py
    • 📄 設置.py

    • 📄 urls.py

    • 📄wsgi.py

  • 📄 texto.md

src/_初始化_.py :

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)

源代碼/芹菜.py:

import os

from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings')
app = Celery('src')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

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

src/settings.py :


INSTALLED_APPS = [
    #Django apps :
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #Meus apps : 
    'src.core',
    'src.project_auth',
    'src.product',
    #Apps de terceiros :   
    'django_celery_results',
    'rest_framework',
    "rest_framework.authtoken",    
]

CELERY_CONFIG = {
    "CELERY_TASK_SERIALIZER": "json",
    "CELERY_ACCEPT_CONTENT": ["json"],
    "CELERY_RESULT_SERIALIZER": "json",
    "CELERY_RESULT_BACKEND": None,
    "CELERY_TIMEZONE": "UTC",
    "CELERY_ENABLE_UTC": True,
    "CELERY_ENABLE_REMOTE_CONTROL": False,
}

docker-compose.yml:

version: "3.8"

services:
  
  web:    
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
    tty: true
    container_name: ${COMPOSE_PROJECT_NAME}_app

  db:
    container_name: ${COMPOSE_PROJECT_NAME}_db
    image: postgres:14
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/

  cache:
    container_name: ${COMPOSE_PROJECT_NAME}_celery_broker
    image: rabbitmq:3.9
    ports:
      - 5672:5672
    
volumes:
  postgres_data:

celery --app src.project_auth bash 上,我運行celery --app src.project_auth並得到:

Error: Invalid value for '-A' / '--app': 
Unable to load celery application.
Module 'src.project_auth' has no attribute 'celery'

謝謝你的幫助 !

看起來您的celery.py文件位於錯誤的位置,從錯誤消息中您應該將celery.py移動到project_auth文件夾中。

Module 'src.project_auth' has no attribute 'celery'

這表明 Celery 期待src/project_auth/celery.py而你有src/celery.py

另一種選擇是將 bash 腳本更改為

celery --app src

祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM