kev*_*kev 41 python django importerror celery
我在这里看过这个例子:
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
我的所有任务都在名为tasks.py的文件中.
更新芹菜并从示例中添加文件django抛出以下错误,无论我尝试什么:
ImportError: cannot import name Celery
Run Code Online (Sandbox Code Playgroud)
问题可能是由以下原因引起的吗?
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
Run Code Online (Sandbox Code Playgroud)
因为它遍历所有具有以下导入的tasks.py文件.
from cloud.celery import app
Run Code Online (Sandbox Code Playgroud)
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
)
Run Code Online (Sandbox Code Playgroud)
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')
Run Code Online (Sandbox Code Playgroud)
当我尝试访问无效的URL(如/ foobar)时,会发生错误.
这是完整的追溯:
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
Run Code Online (Sandbox Code Playgroud)
kev*_*kev 56
将以下行添加到cloud/celery.py:
import celery
print celery.__file__
Run Code Online (Sandbox Code Playgroud)
给了我文件本身而不是库中的芹菜模块.将celery.py重命名为celeryapp.py并调整导入后,所有错误都消失了.
注意:
这导致了启动工人的变化:
celery worker --app=cloud.celeryapp:app
Run Code Online (Sandbox Code Playgroud)
对于那些运行celery == 3.1.2并且收到此错误的人:
TypeError: unpack_from() argument 1 must be string or read-only buffer, not memoryview
Run Code Online (Sandbox Code Playgroud)
应用此处提到的补丁:https://github.com/celery/celery/issues/1637
jwd*_*630 43
使用Django 1.7.5,Celery 3.1.17和Python 2.7.6,我发现我还在使用它们ImportError: cannot import name Celery.但只有在PyCharm 4.0.4下运行测试时.
我发现解决方案不依赖于Django的第一步中from __future__ import absolute_import所描述的.相反,我重命名为然后更改匹配的内容:.不再有多个命名导致导入混淆的文件实例似乎是一种更简单的方法.proj/proj/celery.pyproj/proj/celery_tasks.py__init__.pyfrom .celery_tasks import app as celery_appcelery.py
小智 8
得到了同样的错误
我的芹菜设置文件名(celery.py)与'celery'包有冲突...
所以当做这个 - >从芹菜进口芹菜,它提出错误 - 不能导入名称芹菜
解决方案 - >只需将'celery.py'更改为'celery-settings.py'之类的其他内容
你添加了这条线:
from __future__ import absolute_import
到你的cloud/celery.py模块的顶部?
阅读此处示例的细分:http: //docs.celeryproject.org/en/latest/django/first-steps-with-django.html
对于想知道导致此错误的原因的人:
我刚才遇到了这个问题,然后我发现了问题---sys.path。
也许你像我一样向 sys.path 添加一些路径,我在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)
Run Code Online (Sandbox Code Playgroud)
所以,会在和from celery import Celery中搜索 celerySRC_PATHCONF_PATH,这就是问题所在。
改成
sys.path.append(SRC_PATH)
sys.path.append(CONF_PATH)
Run Code Online (Sandbox Code Playgroud)
lib它将首先在 python 中进行搜索site-packages。完美解决。
小智 5
我得到了同样的错误。原来我的 Celery 版本有问题。我升级到 3.1,现在这个版本不推荐使用celeryd(http://celery.readthedocs.org/en/latest/whatsnew-3.1.html)。所以我不得不降级到 3.0.19 版本,这是该项目以前使用的稳定版本,到目前为止它运行良好。
pip install celery==3.0.19
Run Code Online (Sandbox Code Playgroud)
无论如何,如果你不想降级,3.1 版本中 celeryd 的替代品是celery worker。在此处查看更多信息:http : //celery.readthedocs.org/en/latest/userguide/workers.html。
希望这可以帮助!:)
| 归档时间: |
|
| 查看次数: |
38731 次 |
| 最近记录: |