Django Celery 设置导入问题

5 投票
1 回答
2453 浏览
提问于 2025-04-18 07:41

我正在尝试创建一个celery文件并通过..来运行它。

celery -A myapp worker -l info

但是我遇到了这个错误,

  File "/production/pythonenv/project/lib/python2.7/site-packages/configurations/base.py", line 30, in __new__
    raise ImproperlyConfigured(install_failure)
django.core.exceptions.ImproperlyConfigured: django-configurations settings importer wasn't correctly installed. Please use one of the starter functions to install it as mentioned in the docs: http://django-configurations.readthedocs.org/

以下是配置和文件,

目录结构

(从项目根目录通过 'tree myapp/ config/' 命令运行)

config/
|-- __init__.py
|-- __init__.pyc
|-- celery.py
|-- settings.py
|-- urls.py
|-- wsgi.py  
myapp/
|-- __init__.py
|-- __init__.pyc
|-- celery.py
|-- helper.py
|-- models.pyc
|-- serializers.py
|-- views.py

celery.py

from __future__ import absolute_import
import os,sys,
from celery import Celery
from django.conf import settings

sys.path.append('/prod/project/')
sys.path.append('/prod/')

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Local')

app = Celery('myapp.celery.celery')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')

有什么想法吗?

1 个回答

15

根据这个链接,你可能需要在你的celery.py文件中添加一些内容:

os.environ.setdefault('DJANGO_CONFIGURATION', 'Local')

from configurations import importer
importer.install()

app = Celery('myapp.celery.celery')

撰写回答