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

Django项目配置Celery定时任务遇ImportError: No module named celery求助

Fixing ImportError: No module named celery in a Django Project with Nested Configs

It sounds like your nested config structure (local.py/production.py in a subfolder) is likely throwing off Celery's ability to find the right modules. Let's walk through targeted fixes:

1. Verify Your celery.py Location & Configuration

First, make sure your celery.py is placed correctly and pointing to your nested settings. For example, if your project structure looks like this:

your_project/
    config/
        __init__.py
        local.py
        production.py
    your_project/
        __init__.py
        celery.py
    manage.py

Update the DJANGO_SETTINGS_MODULE in celery.py to match your nested config path:

import os
from celery import Celery

# Point to your nested settings file instead of the default
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.local')  # or 'config.production'

app = Celery('your_project')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

2. Check Your __init__.py Setup

In the same directory as celery.py (usually your project's inner root folder), ensure your __init__.py imports the Celery app so Django can load it properly:

from .celery import app as celery_app

__all__ = ('celery_app',)

3. Fix Python Path & Worker Command

If you're running the Celery worker from the wrong directory, it won't find your modules. Always run the command from your project's root folder (where manage.py lives):

celery -A your_project worker --loglevel=info

If your celery.py is inside the config folder instead, adjust the -A argument to match:

celery -A config worker --loglevel=info

If you still have path issues, explicitly add your project root to the Python path before running the worker:

export PYTHONPATH=/absolute/path/to/your/project/root
celery -A your_project worker --loglevel=info

4. Confirm Celery is Installed in the Right Environment

Double-check that Celery is installed in the virtual environment you're using for your Django project. Run this in your terminal:

pip list | grep celery

If it doesn't show up, reinstall it in your active environment:

pip install celery

5. Validate Celery Config in Your Nested Settings

Make sure your local.py or production.py includes basic Celery configuration (even if minimal):

# Example broker/backend config (adjust to your setup)
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

Most of the time, this error boils down to misaligned paths between your nested configs and Celery's expected module structure. Start with adjusting the DJANGO_SETTINGS_MODULE in celery.py and running the worker from the correct directory—those are the most common fixes here.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:34:16