我在这里看过这个例子:
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
我的所有任务都在名为tasks.py的文件中 .
更新芹菜并从示例中添加文件django抛出以下错误,无论我尝试什么:
ImportError: cannot import name Celery
问题可能是由以下原因引起的吗?
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
因为它遍历所有具有以下导入的tasks.py文件 .
from cloud.celery import app
cloud/celery.py :
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.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
)
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')
当我尝试访问无效的URL(如/ foobar)时,会发生错误 .
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
9 回答
我得到了同样的错误 . 事实证明我的Celery版本存在问题 . 我升级到3.1并且 celeryd 现在已弃用此版本(http://celery.readthedocs.org/en/latest/whatsnew-3.1.html) . 所以我不得不降级到3.0.19版本,这是之前用于该项目的稳定版本,到目前为止它运行良好 .
无论如何,如果你不想降级,3.1版本中的celeryd替换是 celery worker . 点击此处查看更多信息:http://celery.readthedocs.org/en/latest/userguide/workers.html .
希望这可以帮助! :)
我得到了同样的错误 .
似乎
from __future__ import absolute_import
不适用于Python 2.6.1,仍然没有引发错误 .升级到Python 2.7.5,它刚刚起作用 .
将以下行添加到cloud / celery.py:
给了我文件本身而不是库中的芹菜模块 . 将celery.py重命名为celeryapp.py并调整导入后,所有错误都消失了 .
注意:
这导致了启动 Worker 的变化:
对于那些运行celery == 3.1.2并且收到此错误的人:
应用此处提到的补丁:https://github.com/celery/celery/issues/1637
使用Django 1.7.5,Celery 3.1.17和Python 2.7.6,我发现我还是得到了这些
ImportError: cannot import name Celery
. 但只有在PyCharm 4.0.4下运行测试时 .我发现 not 的解决方案依赖于
from __future__ import absolute_import
,如First Steps with Django所述 . 相反,我将proj/proj/celery.py
重命名为proj/proj/celery_tasks.py
,然后将__init__.py
的内容更改为匹配:from .celery_tasks import app as celery_app
. 不再有多个名为celery.py
的文件实例导致导入混乱似乎是一种更简单的方法 .你添加了这条线:
from __future__ import absolute_import
到
cloud/celery.py
模块的顶部?阅读此处示例的细分:http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
为我工作(部署在服务器后的一些错误):从项目中删除所有* .pyc文件并重新启动它 .
得到了同样的错误
我的芹菜设置文件名(celery.py)与'celery'包有冲突...
所以当做这个 - >从芹菜进口芹菜,它提出错误 - 不能导入名称芹菜
解决方案 - >只需将'celery.py'更改为'celery-settings.py'之类的其他内容
For someone who want to know what cause this error :
我刚才遇到这个问题,然后我发现了问题--- sys.path .
也许你像我一样添加一些sys.path的路径,我在manage.py中添加以下代码,
所以,
from celery import Celery
会首先在SRC_PATH
和CONF_PATH
搜索芹菜,这就是问题所在 .改成
它将首先搜索python的
lib
和site-packages
. 完美解决了 .请注意,较旧的Django项目将
manage.py
脚本与项目目录放在同一目录中 . 也就是说,结构如下所示:而不是这个:
在这种情况下,您只需将
celery.app
文件重命名为不同的文件,如上面接受的答案中建议的celeryapp.py
.