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

首次使用Django-Celery遇ImportError:找不到MyProject模块

Fixing "ImportError: No module named MyProject" in Django-Celery

Hey there, let's work through this ImportError you're hitting when trying to run your Celery worker. This is a super common path configuration issue, so here are the most reliable fixes to get you up and running:

1. Confirm you're in the right working directory

Celery needs to locate your Django project module, which means you must run the command from your project's root directory. Your project structure should look like this:

your-project-root/
    MyProject/          # Your core Django config folder
        __init__.py
        celery.py
        settings.py
    manage.py           # This file lives in the root directory

If you're currently inside the inner MyProject/ folder, move up one level first (run cd .. on Linux/macOS/Windows) before executing the Celery command:

celery -A MyProject worker -l info

2. Add your project root to the Python path

If you're already in the correct directory but still seeing the error, Python might not be detecting your project folder automatically. Fix this by temporarily adding the project root to your PYTHONPATH:

  • Linux/macOS:
    export PYTHONPATH=/full/path/to/your-project-root:$PYTHONPATH
    celery -A MyProject worker -l info
    
  • Windows (Command Prompt):
    set PYTHONPATH=C:\full\path\to\your-project-root;%PYTHONPATH%
    celery -A MyProject worker -l info
    
  • Windows (PowerShell):
    $env:PYTHONPATH = "C:\full\path\to\your-project-root;$env:PYTHONPATH"
    celery -A MyProject worker -l info
    

3. Double-check your celery.py configuration

Make sure your celery.py file correctly sets the Django settings module early on. You should have this line:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyProject.settings')

This tells Celery exactly where to find your Django project's configuration—critical for importing the MyProject module successfully.

4. Test the module import first

Before running Celery, verify Python can actually import your MyProject module. From your project root, run:

python -c "import MyProject"

If this doesn't throw an error, your path is set up correctly, and the Celery command should work. If it does error out, circle back to steps 1 and 2 to fix your path issues.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:34:56