Django, ImportError: cannot import name Celery, possible circular import?


I went through this example here:

All my tasks are in files called

After updating celery and adding the file from the example django is throwing the following error, no matter what I try:

ImportError: cannot import name Celery

Is the problem possibly caused by the following?

app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')

Because it goes through all files which all have the following import.

from cloud.celery import app


from __future__ import absolute_import

import os, sys
from celery import Celery
from celery.schedules import crontab
from django.conf import settings

BROKER_URL = 'redis://:PASSWORD@localhost'

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cloud.settings')
app = Celery('cloud', broker=BROKER_URL)
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')

if "test" in sys.argv:
    print >> sys.stderr, 'CELERY_ALWAYS_EAGER = True'

    'test_rabbit_running': {
        "task": "retail.tasks.test_rabbit_running",
        "schedule": 3600, #every hour



from cloud.celery import app

import logging
from celery.utils.log import get_task_logger
logger = get_task_logger('tasks')

def test_rabbit_running():
    import datetime
    utcnow ='CELERY RUNNING')

The error happens, when I try to access a url that is not valid, like /foobar.

Here is the full traceback:

Traceback (most recent call last):
  File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/gunicorn/workers/", line 126, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/", line 255, in __call__
    response = self.get_response(request)
  File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/", line 178, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/", line 220, in handle_uncaught_exception
    if resolver.urlconf_module is None:
  File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/", line 342, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/", line 35, in import_module
  File "/opt/src/slicephone/cloud/cloud/", line 52, in 
    urlpatterns +=  patterns('', url(r'^search/', include('search.urls')))
  File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/conf/urls/", line 25, in include
    urlconf_module = import_module(urlconf_module)
  File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/", line 35, in import_module
  File "/opt/src/slicephone/cloud/search/", line 5, in 
    from handlers import SearchHandler
  File "/opt/src/slicephone/cloud/search/", line 15, in 
    from places import handlers as placeshandler
  File "/opt/src/slicephone/cloud/places/", line 23, in 
    import api as placesapi
  File "/opt/src/slicephone/cloud/places/", line 9, in 
    from djapi import *
  File "/opt/src/slicephone/cloud/places/", line 26, in 
    from tasks import add_single_place, add_multiple_places
  File "/opt/src/slicephone/cloud/places/", line 2, in 
    from cloud.celery import app
  File "/opt/src/slicephone/cloud/cloud/", line 4, in 
    from celery import Celery
  File "/opt/src/slicephone/cloud/cloud/", line 4, in 
    from celery import Celery
ImportError: cannot import name Celery
11/12/2013 10:49:04 PM

Accepted Answer

Adding the following lines to cloud/

import celery
print celery.__file__

gave me the file itself and not the celery module from the library. After renaming to and adjusting the imports all errors were gone.


That leads to a change in starting the worker:

celery worker --app=cloud.celeryapp:app

For those running celery==3.1.2 and getting this error:

TypeError: unpack_from() argument 1 must be string or read-only buffer, not memoryview

Apply the patch mentioned here:

11/13/2013 12:18:44 AM

With Django 1.7.5, Celery 3.1.17, and Python 2.7.6 I found that I was still getting these ImportError: cannot import name Celery. But only when running tests under PyCharm 4.0.4.

I found that a solution was not to rely on from __future__ import absolute_import as described in First Steps with Django. Instead I renamed proj/proj/ to proj/proj/ and then changed the content of to match: from .celery_tasks import app as celery_app. No more multiple instances of files named to cause import confusion seemed to be a simpler approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow