Celery, kombu and django - import error

2019-06-21 23:02发布

问题:

I am running an application with django, and I wanted to use celery to make some scheduled tasks.

According to the oficial docs, in my settings.py file I set the broker transport

BROKER_URL = 'django://'

and added kombu.transport.django to installed apps

INSTALLED_APPS = (
    ....
    'kombu.transport.django',
    ....)

However, when I try to sync the database, with python manage.py syncdb, i get the following error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 312, in execute
    django.setup()
  File "C:\Python27\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python27\lib\site-packages\django\apps\registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "C:\Python27\lib\site-packages\django\apps\config.py", line 119, in create
    import_module(entry)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named django

Any ideas why I'm getting this? I have django installed correctly and the application runs fine if I remove kombu.transport.django from installed apps.

Edit: this seems to be a problem with kombu. I installed it with pip install kombu. When I try to import in a file 'kombu.transport' I get no errors, but when I try to import 'kombu.transport.django' I get an erro:

ImportError: No module named django

回答1:

While it might have happened after your post, the django module was definitely removed from the kombu package. Current documentation for celery states that as of version 3.1, celery works with Django 'out of the box'. That means you don't have to modify settings.py to integrate celery (so don't include references to kombu).

Removing references to kombu in settings.py is the correct solution.

This is the commit: https://github.com/celery/kombu/commit/65f982ccf31b86157c39a8feb42081410b83abe2



回答2:

latest celery has removed support for django as broker. They prefer redis,rabbitmq as their preferred broker.