Django-celery-results: error ModuleNotFoundError: No module named 'django_celery_results'
I try to run "celery -A myproj worker -l info" and it gives me the error ModuleNotFoundError: No module named 'django_celery_results'. All dependencies are installed (django, celery, django-celery-results, redis). I tried to run it with admin rights, but it didn't help as well. I tried changing the python version from 3.13 to 3.11, but also without result.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_celery_results',]
This error usually happens when Celery can't find the Django environment. Here are the steps to fix it:
Step 1: Activate your virtual environment first
source your_venv/bin/activate # On Linux/Mac
your_venv\Scripts\activate # On Windows
Step 2: Check your celery.py file
Make sure you have a celery.py
file in your project directory (same level as settings.py
):
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings')
app = Celery('myproj')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
Step 3: Update settings.py
Add these configurations to your Django settings:
# Add to INSTALLED_APPS
INSTALLED_APPS = [
# ... other apps
'django_celery_results',
]
# Add Celery config
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'django-db'
Step 4: Run migrations
python manage.py migrate django_celery_results
Step 5: Start Celery from project directory
cd /path/to/your/project
celery -A myproj worker -l info
You can also try:
celery -A myproj.celery worker -l info
Alternative: Use Redis for Results (Optional)
If you're still having issues, you can skip django-celery-results entirely:
# In settings.py
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
# Remove from INSTALLED_APPS
# 'django_celery_results',
Debug Test
To verify the package is accessible:
python -c "import django_celery_results; print('Package found')"
The main issue is usually running Celery outside the virtual environment or missing the Django setup in celery.py. Make sure you're running all commands from within your activated virtual environment and from your project's root directory.