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

部署至Azure WebApp后无法导入Celery的问题排查求助

排查与解决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 celery locally 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.py files 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 celery submodule (e.g., an app directory with its own celery.py or celery/ 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.py to 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 in wsgi.py before 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.txt to include celery==4.4.7, redeploy, and test.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:11:37