importerror: cannot import name celery

  • Last Update :
  • Techknowledgy :

Adding the following lines to cloud/celery.py:

import celery
print celery.__file__

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

I got the same error. It turns out there was a problem with my Celery version. I upgraded to 3.1 and celeryd is now deprecated for this version (http://celery.readthedocs.org/en/latest/whatsnew-3.1.html). So I had to downgrade to the version 3.0.19 that was the previous stable version used for the project, and it works good so far.

    pip install celery == 3.0 .19

For someone who want to know what cause this error:
I have meet this problem just now, then I found the problem --- sys.path.
Maybe you add some path to sys.path like me, I add below code in manage.py,

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
SRC_PATH = os.path.join(ROOT_PATH, 'src')
CONF_PATH = os.path.join(ROOT_PATH, 'conf')

sys.path.insert(0, SRC_PATH)
sys.path.insert(0, CONF_PATH)

change to

sys.path.append(SRC_PATH)
sys.path.append(CONF_PATH)

Note that older Django projects have the manage.py script in the same directory as the project directory. That is, the structure looks like this:

-proj /
   -proj / __init__.py -
   proj / celery.py -
   proj / urls.py -
   proj / manage.py -
   proj / settings.py

instead of this:

-proj /
   -proj / __init__.py -
   proj / celery.py -
   proj / settings.py -
   proj / urls.py -
   manage.py

Suggestion : 2

Post published:August 17, 2021

After updating celery and adding the file from the example django is throwing the following error, no matter what 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 tasks.py files which all have the following import.

from cloud.celery
import app

retail/tasks.py:

from cloud.celery
import app

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

@app.task
def test_rabbit_running():
   import datetime
utcnow = datetime.datetime.now()
logger.info('CELERY RUNNING')

Here is the full traceback:

Traceback(most recent call last):
   File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", 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/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py", 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/base.py", 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/urlresolvers.py", 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/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/cloud/urls.py", 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/__init__.py", line 25, in include
urlconf_module = import_module(urlconf_module)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/search/urls.py", line 5, in
from handlers
import SearchHandler
File "/opt/src/slicephone/cloud/search/handlers.py", line 15, in
from places
import handlers as placeshandler
File "/opt/src/slicephone/cloud/places/handlers.py", line 23, in
import api as placesapi
File "/opt/src/slicephone/cloud/places/api.py", line 9, in
from djapi
import *
File "/opt/src/slicephone/cloud/places/djapi.py", line 26, in
from tasks
import add_single_place, add_multiple_places
File "/opt/src/slicephone/cloud/places/tasks.py", line 2, in
from cloud.celery
import app
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery
import Celery
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery
import Celery
ImportError: cannot
import name Celery

Suggestion : 3

In this case, you will just have to rename the celery.app file to something different, like celeryapp.py as suggested in the accepted answer above.,gave 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.,Another simple way to solve this: If your package have celery configuration in celery.py this is the reason that it is causing problems. Rename it something like celery_settings.py,Note that older Django projects have the manage.py script in the same directory as the project directory. That is, the structure looks like this:

Adding the following lines to cloud/celery.py:

import celery
print celery.__file__

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

Note that older Django projects have the manage.py script in the same directory as the project directory. That is, the structure looks like this:

-proj /
   -proj / __init__.py -
   proj / celery.py -
   proj / urls.py -
   proj / manage.py -
   proj / settings.py

instead of this:

-proj /
   -proj / __init__.py -
   proj / celery.py -
   proj / settings.py -
   proj / urls.py -
   manage.py

For someone who want to know what cause this error:
I have meet this problem just now, then I found the problem --- sys.path.
Maybe you add some path to sys.path like me, I add below code in manage.py,

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
SRC_PATH = os.path.join(ROOT_PATH, 'src')
CONF_PATH = os.path.join(ROOT_PATH, 'conf')

sys.path.insert(0, SRC_PATH)
sys.path.insert(0, CONF_PATH)

change to

sys.path.append(SRC_PATH)
sys.path.append(CONF_PATH)

I got the same error. It turns out there was a problem with my Celery version. I upgraded to 3.1 and celeryd is now deprecated for this version (http://celery.readthedocs.org/en/latest/whatsnew-3.1.html). So I had to downgrade to the version 3.0.19 that was the previous stable version used for the project, and it works good so far.

    pip install celery == 3.0 .19

Suggestion : 4

Change your in folder celery.py to anycodings_importerror something else like _celery.py, then try anycodings_importerror the import. It should work. The error is anycodings_importerror because the your celery.py file clashes anycodings_importerror with the celery module.,It is the first time I use celery, I went anycodings_django through this tutorial: Using celery with anycodings_django Django but when I run the code it gives me anycodings_django the following error :,Then you should be fine and does not anycodings_importerror need to change the name of your file.,You need to put this line of code in the anycodings_importerror top of your file.

It is the first time I use celery, I went anycodings_django through this tutorial: Using celery with anycodings_django Django but when I run the code it gives me anycodings_django the following error :

from celery
import Celery
ImportError: cannot
import name Celery

You need to install it:

pip install celery

You need to put this line of code in the anycodings_importerror top of your file.

from __future__
import absolute_import, unicode_literals

Suggestion : 5

gave 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.,After updating celery and adding the file from the example django is throwing the following error, no matter what I try:, Importing the module in a file and only then importing your file in the project's init so celery can work If you want help with the circular import, please post your celery_task_settings.py, celery.py, and django settings files. celery.py would be the standard package. , 3 days ago I am trying to use a custom library in a celery task file,but whenever I am try to start the worker there is a ImportError: cannot import name … Press J to jump to the feed. Press question mark to learn the rest of the keyboard shortcuts


ImportError: cannot
import name Celery

import celery print celery.__file__
ImportError: cannot
import name Celery
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name = 'tasks')
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://:[email protected]'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cloud.settings') app = Celery('cloud', broker = BROKER_URL) app.config_from_object('django.conf:settings') app.autodiscover_tasks(settings.INSTALLED_APPS, related_name = 'tasks') if "test" in sys.argv: app.conf.update(CELERY_ALWAYS_EAGER = True, ) print >> sys.stderr, 'CELERY_ALWAYS_EAGER = True'
CELERYBEAT_SCHEDULE = {
      'test_rabbit_running': {
         "task": "retail.tasks.test_rabbit_running",
         "schedule": 3600,
         #every hour
      },
      [..] app.conf.update(CELERYBEAT_SCHEDULE = CELERYBEAT_SCHEDULE)

Suggestion : 6

A shorten Answer: Your problem here is that you've named a submodule (aka a python file) or a package (aka a folder) with the same name of the package that you want to import celery therefore you need to change the name of this file in order to import the correct package.,More details: Python importing module checks the packages name in the paths specified ordered in sys.path. So if you printed,Django is a free framework for Python-based web applications that uses the MVC design pattern.,Django Rest Framework (DRF) is a library that works with standard Django models to create a flexible and powerful API for a project.

__init__.py file:

from __future__
import absolute_import
from core.celery
import app as celery_app

__all__ = ['celery_app']

celery.py file:

from __future__
import absolute_import
import os
from celery
import Celery

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

app = Celery('core')
app.config_from_object('django.conf:settings', namespace = 'CELERY')
app.autodiscover_tasks()

@app.task(bind = True)
def debug_task(self):
   print('Request: {0!r}'.format(self.request))

task.py file:

from celery
import shared_task
from time
import sleep
from azure.datalake.store
import core, lib, multithread
from django.core.mail
import send_mail
token = lib.auth()
adls_client = core.AzureDLFileSystem(token, store_name = 'bnlweda04d3232gsdfs')

@shared_task
def sleepy(duration):
   sleep(duration)
return None

@shared_task
def send_email_task(subject, message, from_email, recipient_email, fail_silently):
   sleep(30)
send_mail(
   subject, message, from_email, recipient_email, fail_silently
)
return 'Mail sent success'

More details:
Python importing module checks the packages name in the paths specified ordered in sys.path. So if you printed

print(sys.path)

Suggestion : 7

Describe the bug This i think: https://stackoverflow.com/questions/19926750/django-importerror-cannot-import-name-celery-possible-circular-import,Additional context If i run python celery.py in /home/XXXXXX/weblate/weblate/ i get this error aswell: Traceback (most recent call last): File "celery.py", line 26, in <module> from celery import Celery File "/home/XXXXXX/weblate/weblate/celery.py", line 26, in <module> from celery import Celery ImportError: cannot import name 'Celery',not in my uwsgi settings but in my /etc/systemd/system/celery-weblate.service, The WorkingDirectory= had one too many /weblate appended.,Expected behavior Celery service starts without error.

Issue #2420

Signed-off-by: Michal Čihař <michal@cihar.com>

Suggestion : 8

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/celery.py to proj/proj/celery_tasks.py and then changed the content of __init__.py to match: from .celery_tasks import app as celery_app. No more multiple instances of files named celery.py to cause import confusion seemed to be a simpler approach. ,from __future__ import absolute_import,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.,gave 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.

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 tasks.py files which all have the following import.

from cloud.celery
import app

retail/tasks.py:

from cloud.celery
import app

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

@app.task
def test_rabbit_running():
   import datetime
utcnow = datetime.datetime.now()
logger.info('CELERY RUNNING')

Here is the full traceback:

Traceback(most recent call last):
   File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", 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/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py", 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/base.py", 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/urlresolvers.py", 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/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/cloud/urls.py", 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/__init__.py", line 25, in include
urlconf_module = import_module(urlconf_module)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/search/urls.py", line 5, in
from handlers
import SearchHandler
File "/opt/src/slicephone/cloud/search/handlers.py", line 15, in
from places
import handlers as placeshandler
File "/opt/src/slicephone/cloud/places/handlers.py", line 23, in
import api as placesapi
File "/opt/src/slicephone/cloud/places/api.py", line 9, in
from djapi
import *
File "/opt/src/slicephone/cloud/places/djapi.py", line 26, in
from tasks
import add_single_place, add_multiple_places
File "/opt/src/slicephone/cloud/places/tasks.py", line 2, in
from cloud.celery
import app
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery
import Celery
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery
import Celery
ImportError: cannot
import name Celery

Suggestion : 9

gave 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.,Adding the following lines to cloud/celery.py:,After updating celery and adding the file from the example django is throwing the following error, no matter what I try:,Another simple way to solve this: If your package have celery configuration in celery.py this is the reason that it is causing problems. Rename it something like celery_settings.py

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 tasks.py files which all have the following import.

from cloud.celery
import app

retail/tasks.py:

from cloud.celery
import app

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

@app.task
def test_rabbit_running():
   import datetime
utcnow = datetime.datetime.now()
logger.info('CELERY RUNNING')

Here is the full traceback:

Traceback(most recent call last):
   File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", 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/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/core/handlers/base.py", 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/base.py", 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/urlresolvers.py", 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/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/cloud/urls.py", 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/__init__.py", line 25, in include
urlconf_module = import_module(urlconf_module)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/opt/src/slicephone/cloud/search/urls.py", line 5, in
from handlers
import SearchHandler
File "/opt/src/slicephone/cloud/search/handlers.py", line 15, in
from places
import handlers as placeshandler
File "/opt/src/slicephone/cloud/places/handlers.py", line 23, in
import api as placesapi
File "/opt/src/slicephone/cloud/places/api.py", line 9, in
from djapi
import *
File "/opt/src/slicephone/cloud/places/djapi.py", line 26, in
from tasks
import add_single_place, add_multiple_places
File "/opt/src/slicephone/cloud/places/tasks.py", line 2, in
from cloud.celery
import app
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery
import Celery
File "/opt/src/slicephone/cloud/cloud/celery.py", line 4, in
from celery
import Celery
ImportError: cannot
import name Celery

Adding the following lines to cloud/celery.py:

import celery
print celery.__file__

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

I got the same error. It turns out there was a problem with my Celery version. I upgraded to 3.1 and celeryd is now deprecated for this version (http://celery.readthedocs.org/en/latest/whatsnew-3.1.html). So I had to downgrade to the version 3.0.19 that was the previous stable version used for the project, and it works good so far.

    pip install celery == 3.0 .19

For someone who want to know what cause this error:
I have meet this problem just now, then I found the problem --- sys.path.
Maybe you add some path to sys.path like me, I add below code in manage.py,

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
SRC_PATH = os.path.join(ROOT_PATH, 'src')
CONF_PATH = os.path.join(ROOT_PATH, 'conf')

sys.path.insert(0, SRC_PATH)
sys.path.insert(0, CONF_PATH)

change to

sys.path.append(SRC_PATH)
sys.path.append(CONF_PATH)

Note that older Django projects have the manage.py script in the same directory as the project directory. That is, the structure looks like this:

-proj /
   -proj / __init__.py -
   proj / celery.py -
   proj / urls.py -
   proj / manage.py -
   proj / settings.py

instead of this:

-proj /
   -proj / __init__.py -
   proj / celery.py -
   proj / settings.py -
   proj / urls.py -
   manage.py