You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置Celery后Django部署Elastic Beanstalk失败:ImportError无法导入Celery

Fixing Celery ImportError & Startup Issues on Elastic Beanstalk

Alright, let's dig into why your Celery Beat and Worker are failing to start on Elastic Beanstalk (EB) while working perfectly locally. The core error here is ImportError: cannot import name 'Celery'—this is almost always a circular import conflict caused by your project's file naming. Let's walk through the fixes step by step:

1. Resolve the Circular Import Conflict

The problem stems from having a file named celery.py in your Django app (/opt/python/current/app/django_app/celery.py). When you run from celery import Celery, Python tries to import this local file instead of the actual Celery library, leading to the import error.

Fix Steps:

  • Rename your local Celery file: Change /opt/python/current/app/django_app/celery.py to something unique, like celery_app.py.
  • Update imports in your project:
    • Keep your existing Celery setup code (the part that initializes the Celery instance) in the newly renamed celery_app.py.
    • In your Django project's root __init__.py (e.g., djangobrain/__init__.py), update the import to:
      from .celery_app import app as celery_app
      __all__ = ('celery_app',)
      
  • Verify task imports: Make sure all your task files import the Celery app from the renamed file instead of the old celery.py.

2. Confirm Celery is Installed in the EB Virtual Environment

Even if Celery works locally, it might not be installed in EB's virtual environment. Double-check:

  • Ensure celery is listed in your requirements.txt file (use the same version you use locally, e.g., celery==5.3.0).
  • Add a quick verification step to your EB container commands to confirm installation:
    01_verify_celery_install:
      command: "/opt/python/run/venv/bin/pip list | grep celery"
      leader_only: true
    

3. Validate Supervisord Configuration

Your celery_configuration.txt looks mostly correct, but let's confirm a few details:

  • Command paths: The paths /opt/python/run/venv/bin/celery should point to the correct Celery executable in EB's virtual environment. If you renamed your Celery app, ensure the -A djangobrain argument still targets the correct module (your project root where the celery_app is imported).
  • Permissions: The nobody user should have access to the log files and app directory. Test running the Celery command manually via SSH to debug immediate errors:
    sudo su nobody -s /bin/bash
    /opt/python/run/venv/bin/celery worker -A djangobrain --loglevel=INFO
    

4. Clean Python Cache & Redeploy

Old .pyc cache files can sometimes cause lingering import issues. Add a container command to clean them before starting Celery:

03_clean_pycache:
  command: "find /opt/python/current/app -name '*.pyc' -delete"
  leader_only: true

After making these changes, redeploy your EB application. The import error should be resolved, and supervisord should be able to start Celery Beat and Worker successfully.

内容的提问来源于stack exchange,提问作者mrhassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:09:20