Python Django,ImportError:无法导入名称 Celery,可能的循环导入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19926750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django, ImportError: cannot import name Celery, possible circular import?
提问by kev
I went through this example here:
我在这里经历了这个例子:
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
All my tasks are in files called tasks.py.
我所有的任务都在名为 tasks.py 的文件中。
After updating celery and adding the file from the example django is throwing the following error, no matter what I try:
更新 celery 并从示例 django 添加文件后,无论我尝试什么,都会抛出以下错误:
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.
因为它遍历所有具有以下导入的 tasks.py 文件。
from cloud.celery import app
cloud/celery.py:
云/芹菜.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:
零售/任务.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')
The error happens, when I try to access a url that is not valid, like /foobar.
当我尝试访问无效的 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
采纳答案by kev
Adding the following lines to cloud/celery.py:
将以下行添加到 cloud/celery.py:
import celery
print celery.__file__
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.
给了我文件本身而不是库中的 celery 模块。将 celery.py 重命名为 celeryapp.py 并调整导入后,所有错误都消失了。
Note:
笔记:
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:
对于那些运行 celery==3.1.2 并收到此错误的人:
TypeError: unpack_from() argument 1 must be string or read-only buffer, not memoryview
Apply the patch mentioned here: https://github.com/celery/celery/issues/1637
应用这里提到的补丁:https: //github.com/celery/celery/issues/1637
回答by asksol
Did you add the line:
您是否添加了以下行:
from __future__ import absolute_import
from __future__ import absolute_import
to the top of your cloud/celery.py
module?
到cloud/celery.py
模块的顶部?
Read the breakdown of the example here: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
在此处阅读示例的细分:http: //docs.celeryproject.org/en/latest/django/first-steps-with-django.html
回答by alanjds
I got the same error.
我得到了同样的错误。
Seems that from __future__ import absolute_import
DOES NOT work for Python 2.6.1, still not raising an error.
似乎from __future__ import absolute_import
不适用于 Python 2.6.1,仍然没有引发错误。
Upgraded to Python 2.7.5 and it just worked.
升级到 Python 2.7.5,它刚刚工作。
回答by Mithril
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,
对于想知道导致此错误的原因的人:
我刚刚遇到此问题,然后我发现了问题--- 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)
so, from celery import Celery
would search celery in SRC_PATH
and CONF_PATH
first, that's the problem.
因此,from celery import Celery
将搜索芹菜SRC_PATH
和CONF_PATH
第一,这就是问题所在。
change to
改成
sys.path.append(SRC_PATH)
sys.path.append(CONF_PATH)
It would search in python's lib
and site-packages
first. Solved perfectly.
这将在Python的搜索lib
和site-packages
第一。完美解决。
回答by Jordan Reiter
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:
请注意,较旧的 Django 项目的manage.py
脚本与项目目录位于同一目录中。也就是说,结构如下所示:
- 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
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.
在这种情况下,您只需将celery.app
文件重命名为不同的名称,就像celeryapp.py
上面接受的答案中所建议的那样。
回答by jwd630
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.
使用 Django 1.7.5、Celery 3.1.17 和 Python 2.7.6,我发现我仍然得到这些ImportError: cannot import name Celery
. 但仅在 PyCharm 4.0.4 下运行测试时。
I found that a solution was notto 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.
我发现一个解决方案是不依赖Django 的第一步中from __future__ import absolute_import
所述。相反,我重命名为然后将内容更改为 match: 。不再有多个命名的文件实例导致导入混淆似乎是一种更简单的方法。proj/proj/celery.py
proj/proj/celery_tasks.py
__init__.py
from .celery_tasks import app as celery_app
celery.py
回答by Emily
I got the same error. It turns out there was a problem with my Celery version. I upgraded to 3.1 and celerydis 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.
我得到了同样的错误。原来我的 Celery 版本有问题。我升级到 3.1,现在这个版本不推荐使用celeryd(http://celery.readthedocs.org/en/latest/whatsnew-3.1.html)。所以我不得不降级到 3.0.19 版本,这是该项目以前使用的稳定版本,到目前为止它运行良好。
pip install celery==3.0.19
Anyway, if you don't want to downgrade, the replacement for celeryd in the version 3.1 is celery worker. Check here for more info: http://celery.readthedocs.org/en/latest/userguide/workers.html.
无论如何,如果你不想降级,3.1 版本中 celeryd 的替代品是celery worker。在此处查看更多信息:http: //celery.readthedocs.org/en/latest/userguide/workers.html。
Hope this helps! :)
希望这可以帮助!:)
回答by Dmitriy Yusupov
Work for me ( some bug after deploy in server ): Remove all *.pyc files from project and restart him.
为我工作(在服务器中部署后的一些错误):从项目中删除所有 *.pyc 文件并重新启动他。
回答by PRASHANT VERMA
got the same error
得到了同样的错误
my celery settings filename which was(celery.py) was conflicting with 'celery' package...
我的芹菜设置文件名(celery.py)与“芹菜”包冲突...
so while doing this-> from celery import Celery , it was raising error- cannot import name Celery
因此,在执行此操作时 -> from celery import Celery ,它引发了错误 - 无法导入名称 Celery
solution->just change the 'celery.py' to something else like 'celery-settings.py'
解决方案->只需将“celery.py”更改为“celery-settings.py”等其他内容