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

Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • 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. – Håken Lid Mar 07 '21 at 21:14
  • but it does not have any problem when i run `runserver` or `celery` commnd – Santhosh Mar 07 '21 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. – Håken Lid Mar 07 '21 at 21:37
  • Does this answer your question? [Django, ImportError: cannot import name Celery, possible circular import?](https://stackoverflow.com/questions/19926750/django-importerror-cannot-import-name-celery-possible-circular-import) Another: https://stackoverflow.com/questions/40446792/importerror-cannot-import-name-celery also: https://stackoverflow.com/questions/45236100/django-importerror-cannot-import-name-celery – Håken Lid Mar 07 '21 at 21:38
  • then why it works fine in other cases. Thats the main thing – Santhosh Mar 07 '21 at 21:39
  • 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](https://docs.python.org/3/reference/import.html#the-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](https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/) – Håken Lid Mar 07 '21 at 21:45
  • if i name celeryfile differently then how to use celery command, – Santhosh Mar 07 '21 at 21:49
  • `python-3.x uses absolute imports by default`, as per this rule, this is contradicting – Santhosh Mar 07 '21 at 21:56

1 Answers1

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',)
AMG
  • 1,606
  • 1
  • 14
  • 25