配置Celery后Django部署Elastic Beanstalk失败:ImportError无法导入Celery
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.pyto something unique, likecelery_app.py. - Update imports in your project:
- Keep your existing Celery setup code (the part that initializes the
Celeryinstance) in the newly renamedcelery_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',)
- Keep your existing Celery setup code (the part that initializes the
- 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
celeryis listed in yourrequirements.txtfile (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/celeryshould point to the correct Celery executable in EB's virtual environment. If you renamed your Celery app, ensure the-A djangobrainargument still targets the correct module (your project root where thecelery_appis imported). - Permissions: The
nobodyuser 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

