0

I am trying to run a standalone Django scipt

import os, sys, django
proj_path = "/path/to/django-project"

import ipdb; ipdb.set_trace()
# This is so Django knows where to find stuff.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "boiler.settings")
sys.path.append(proj_path)
django.setup()

When i run It says

ImportError: cannot import name 'Celery' from 'celery' (/path/to/django-poject/boiler/celery.py)

My folder structure:

django-poject
    -- boiler
        -- __init__.py
        -- settings.py
        -- celery.py
    -- manage.py

__init__.py

from .celery import app as celery_app

__all__ = ['celery_app']

celery.py

import os
from celery import Celery
import django

import sys

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


#This defines the celery app instance
redis = 'redis://:pass@localhost:6379/0'
app = Celery(dirname, 
    broker=redis, 
    backend=redis
    )

I am able to run celery using

activate virtualenv

cd to django-poject

celery -A boiler worker --loglevel=debug

without any problems

But in standalone its creating problems

8
  • You should not name your file celery.py, since that shadows the name of the third party module. I'm not sure if that's what's causing the problem in your case, but it is a frequent cause of ImportErrors. Commented Mar 7, 2021 at 21:14
  • but it does not have any problem when i run runserver or celery commnd Commented Mar 7, 2021 at 21:19
  • If I knew exactly what was wrong, I would probably have submitted an answer. I just have a hunch. This error is typical of a circular import, which is often caused by clashing module names. There are many similar questions already on stackoverflow. I don't understand why the celery docs recommends naming the file celery.py. I think it's good practice to avoid overlapping module names. Commented Mar 7, 2021 at 21:37
  • Does this answer your question? Django, ImportError: cannot import name Celery, possible circular import? Another: stackoverflow.com/questions/40446792/… also: stackoverflow.com/questions/45236100/… Commented Mar 7, 2021 at 21:38
  • 1
    I don't know. ImportErrors can be confusing. My guess is that when you run the celery command, celery is already in python's module cache. It probably matters in which order things happen. This is one of the reasons why the recommended way to implement django standalone scripts is as management commands Commented Mar 7, 2021 at 21:45

1 Answer 1

1

You have to name your celery.py something else. Like django_celery.py otherwise it won't work. Celery works fine without it that way, but you want to integrate in with django and like what Santhosh said, the absolute import of itself is giving you issues.

In your project's __init__.py you'll need something like:

from __future__ import absolute_import, unicode_literals
from your_path_to.django_celery import app as celery_app

__all__ = ('celery_app',)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.