{"id":931,"hash":"49ea25620ce52cae9ad1a3d1a04abde83a0b2b4884e15b7eb70951aa1597acad","pattern":"Django, ImportError: cannot import name Celery, possible circular import?","full_message":"I went through this example here:\n\nhttp://docs.celeryproject.org/en/latest/django/first-steps-with-django.html\n\nAll my tasks are in files called tasks.py.\n\nAfter updating celery and adding the file from the example django is throwing the following error, no matter what I try:\n\nImportError: cannot import name Celery\n\nIs the problem possibly caused by the following?\n\napp.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')\n\nBecause it goes through all tasks.py files which all have the following import.\n\nfrom cloud.celery import app\n\ncloud/celery.py:\n\nfrom __future__ import absolute_import\n\nimport os, sys\nfrom celery import Celery\nfrom celery.schedules import crontab\nfrom django.conf import settings\n\nBROKER_URL = 'redis://:PASSWORD@localhost'\n\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cloud.settings')\napp = Celery('cloud', broker=BROKER_URL)\napp.config_from_object('django.conf:settings')\napp.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')\n\nif \"test\" in sys.argv:\n    app.conf.update(\n        CELERY_ALWAYS_EAGER=True,\n    )\n    print >> sys.stderr, 'CELERY_ALWAYS_EAGER = True'\n\nCELERYBEAT_SCHEDULE = {\n    'test_rabbit_running': {\n        \"task\": \"retail.tasks.test_rabbit_running\",\n        \"schedule\": 3600, #every hour\n    },\n    [..]\n\napp.conf.update(\n    CELERYBEAT_SCHEDULE=CELERYBEAT_SCHEDULE\n)\n\nretail/tasks.py:\n\nfrom cloud.celery import app\n\nimport logging\nfrom celery.utils.log import get_task_logger\nlogger = get_task_logger('tasks')\nlogger.setLevel(logging.DEBUG)\n\n@app.task\ndef test_rabbit_running():\n    import datetime\n    utcnow = datetime.datetime.now()\n    logger.info('CELERY RUNNING')\n\nThe error happens, when I try to access a url that is not valid, like /foobar.\n\nHere is the full traceback:\n\nTraceback (most recent call last):\n  File \"/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/gunicorn/workers/sync.py\", line 126, in handle_request\n    respiter = self.wsgi(environ, resp.start_response)\n  File \"/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py\", line 255, in __call__\n    response = self.get_response(request)\n  File \"/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py\", line 178, in get_response\n    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())\n  File \"/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py\", line 220, in handle_uncaught_exception\n    if resolver.urlconf_module is None:\n  File \"/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/urlresolvers.py\", line 342, in urlconf_module\n    self._urlconf_module = import_module(self.urlconf_name)\n  File \"/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py\", line 35, in import_module\n    __import__(name)\n  File \"/opt/src/slicephone/cloud/cloud/urls.py\", line 52, in \n    urlpatterns +=  patterns('', url(r'^search/', include('search.urls')))\n  File \"/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/conf/urls/__init__.py\", line 25, in include\n    urlconf_module = import_module(urlconf_module)\n  File \"/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py\", line 35, in import_module\n    __import__(name)\n  File \"/opt/src/slicephone/cloud/search/urls.py\", line 5, in \n    from handlers import SearchHandler\n  File \"/opt/src/slicephone/cloud/search/handlers.py\", line 15, in \n    from places import handlers as placeshandler\n  File \"/opt/src/slicephone/cloud/places/handlers.py\", line 23, in \n    import api as placesapi\n  File \"/opt/src/slicephone/cloud/places/api.py\", line 9, in \n    from djapi import *\n  File \"/opt/src/slicephone/cloud/places/djapi.py\", line 26, in \n    from tasks import add_single_place, add_multiple_places\n  File \"/opt/src/slicephone/cloud/places/tasks.py\", line 2, in \n    from cloud.celery import app\n  File \"/opt/src/slicephone/cloud/cloud/celery.py\", line 4, in \n    from celery import Celery\n  File \"/opt/src/slicephone/cloud/cloud/celery.py\", line 4, in \n    from celery import Celery\nImportError: cannot import name Celery","ecosystem":"pypi","package_name":"django","package_version":null,"solution":"Adding the following lines to cloud/celery.py:\n\nimport celery\nprint celery.__file__\n\ngave me the file itself and not the celery module from the library. After renaming celery.py to celeryapp.py and adjusting the imports all errors were gone.\n\nNote:\n\nThat leads to a change in starting the worker:\n\ncelery worker --app=cloud.celeryapp:app\n\nFor those running celery==3.1.2 and getting this error:\n\nTypeError: unpack_from() argument 1 must be string or read-only buffer, not memoryview\n\nApply the patch mentioned here: https://github.com/celery/celery/issues/1637","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/19926750/django-importerror-cannot-import-name-celery-possible-circular-import","votes":58,"created_at":"2026-04-19T04:52:02.700681+00:00","updated_at":"2026-04-19T04:52:02.700681+00:00"}