部署至Azure WebApp后无法导入Celery的问题排查求助
Hey, I’ve run into almost identical deployment headaches with Django + Celery on Azure WebApps before. Let’s break this down step by step to track down the root cause:
1. Verify Celery Version & Installation Integrity
The most common culprit here is a version mismatch between your local venv and Azure’s antenv. Different Celery versions have different structures in celery/__init__.py, which can cause the Celery class import to fail even if the file exists.
- First, run
pip freeze | grep celerylocally to note your working version. - Jump into Kudu for your Azure WebApp, run
/home/site/wwwroot/antenv/bin/pip freeze | grep celery, and compare the version numbers. - If they don’t match, explicitly pin the working version in your
requirements.txt(e.g.,celery==5.2.7) and redeploy. - If the version is correct but the install seems corrupted, force a reinstall via Kudu:
cd /home/site/wwwroot && antenv/bin/pip install -r requirements.txt --force-reinstall
2. Check for Naming Conflicts in Your Project
Even though you renamed celery.py to celery_setup.py, leftover files or conflicting modules can still throw off imports:
- Use Kudu to search for any stray
celery.pyfiles in your project:find /home/site/wwwroot -name "celery.py" - Delete or rename any unexpected matches (old deployment artifacts are a common offender here).
- Also, double-check that none of your Django apps have a
celerysubmodule (e.g., an app directory with its owncelery.pyorcelery/folder).
3. Confirm Python Path Configuration
Azure’s WebApp environment might not be adding your project root to sys.path correctly, which can lead to unexpected module resolution:
- Add temporary debug code at the top of your
wsgi.pyto log path details (deploy and check the application logs afterward):import sys import celery print(f"sys.path contents: {sys.path}") print(f"Celery module being loaded from: {celery.__file__}") - If your project root isn’t in
sys.path, manually add it inwsgi.pybefore loading Django:import os import sys # Add project root to Python path sys.path.append(os.path.dirname(os.path.abspath(__file__))) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings') # ... rest of your wsgi code ...
4. Validate the Azure WebApp Startup Command
Sometimes Azure’s default startup command skips activating the virtual environment, causing the system Python (not your antenv) to be used:
- Head to the Azure Portal, navigate to your WebApp’s Configuration > General settings, and check the "Startup command". It should look something like this:
source antenv/bin/activate && gunicorn --bind=0.0.0.0 --timeout 600 your_project.wsgi:application - Make sure the command explicitly activates the antenv before starting the WSGI server.
5. Try Downgrading Celery (Last Resort)
Python 3.7 is end-of-life, and newer Celery versions (5.x+) have dropped full support for it. If all else fails, try pinning to a 4.x version of Celery that’s compatible with Python 3.7:
- Update your
requirements.txtto includecelery==4.4.7, redeploy, and test.
内容的提问来源于stack exchange,提问作者Ross Halliday

