[英]ImportError with Django 1.8.5 and Celery
I'm trying to get Celery to work Django, following the official documentation, and the video located here: https://godjango.com/63-deferred-tasks-and-scheduled-jobs-with-celery-31-django-17-and-redis/我正在尝试按照官方文档和位于此处的视频让 Celery 使用 Django: https : //godjango.com/63-deferred-tasks-and-scheduled-jobs-with-celery-31-django- 17-and-redis/
I don't know what I'm doing wrong, but this always results in an ImportError.我不知道我做错了什么,但这总是导致导入错误。
The project is called "clubmgmt"该项目名为“clubmgmt”
Contents of "clubmgmt/celery.py" : “clubmgmt/celery.py”的内容:
from __future__ import absolute_import
import os
import django
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'clubmgmt.settings')
django.setup()
app = Celery('clubmgmt')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
Contents of "clubmgmt/__init__.py" “clubmgmt/__init__.py”的内容
from __future__ import absolute_import
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from clubmgmt.celery import app as celery_app
the task is defined in an app called "activation"该任务在名为“激活”的应用程序中定义
Contents of "activation/tasks.py": “activation/tasks.py”的内容:
from activation.models import Activation
from django.conf import settings
from django.template.loader import render_to_string
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from clubmgmt.celery import app
__author__ = 'jeroenjacobs'
@app.task
def send_activation_mail(activation_pk):
activation = Activation.objects.get(pk=activation_pk)
mail = activation.user.email
msg = MIMEMultipart('alternative')
msg['Subject'] = "Please activate your account"
msg['From'] = settings.SMTP_SENDER_ADDRESS
msg['To'] = mail
...
I always receive the following error:我总是收到以下错误:
(clubmgmt) > $ python manage.py runserver [±master ●●●]
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/jeroenjacobs/.pyenv/versions/clubmgmt/lib/python3.4/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
utility.execute()
File "/Users/jeroenjacobs/.pyenv/versions/clubmgmt/lib/python3.4/site-packages/django/core/management/__init__.py", line 303, in execute
settings.INSTALLED_APPS
File "/Users/jeroenjacobs/.pyenv/versions/clubmgmt/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/Users/jeroenjacobs/.pyenv/versions/clubmgmt/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/Users/jeroenjacobs/.pyenv/versions/clubmgmt/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/Users/jeroenjacobs/.pyenv/versions/3.4.3/Python.framework/Versions/3.4/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Users/jeroenjacobs/PycharmProjects/clubmgmt/clubmgmt/__init__.py", line 5, in <module>
from clubmgmt.celery import app as celery_app
File "/Users/jeroenjacobs/PycharmProjects/clubmgmt/clubmgmt/celery.py", line 9, in <module>
django.setup()
File "/Users/jeroenjacobs/.pyenv/versions/clubmgmt/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/jeroenjacobs/.pyenv/versions/clubmgmt/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/Users/jeroenjacobs/PycharmProjects/clubmgmt/activation/apps.py", line 10, in ready
import activation.handlers
File "/Users/jeroenjacobs/PycharmProjects/clubmgmt/activation/handlers.py", line 6, in <module>
from activation.utils import create_or_update_activation
File "/Users/jeroenjacobs/PycharmProjects/clubmgmt/activation/utils.py", line 2, in <module>
from activation.tasks import send_activation_mail
File "/Users/jeroenjacobs/PycharmProjects/clubmgmt/activation/tasks.py", line 7, in <module>
from clubmgmt.celery import app
ImportError: cannot import name 'app'
Can someone please explain me why "app" cannot be imported?有人可以解释一下为什么无法导入“应用程序”吗?
BTW: This is tested under python 3.4.顺便说一句:这是在 python 3.4 下测试的。 Going back to Python 2 is not an option, so please don't suggest this as a solution.
回到 Python 2 不是一种选择,所以请不要建议将其作为解决方案。 If Celery is not Python3 compatible, are there any alternatives?
如果 Celery 与 Python3 不兼容,还有其他选择吗?
The problem is that clubmgmt.celery.app
tries to import clubmgm.celery.app
resulting in a circular import.问题是
clubmgmt.celery.app
试图导入clubmgm.celery.app
导致循环导入。
If you look at your stack trace clubmgmt.__init__
imports clubmgmt.celery.app
, which in turns makes django
run its setup routine which ends up triggering the import of clubmgmt.activation.apps
which at some point through clubmgmt.activation.tasks
imports clubmgmt.celery.app
again, while it's still being imported, resulting in an ImportError
.如果您查看堆栈跟踪
clubmgmt.__init__
导入clubmgmt.celery.app
,这反过来使django
运行其设置程序,最终触发clubmgmt.activation.apps
的导入,在某些时候通过clubmgmt.activation.tasks
导入clubmgmt.celery.app
再次导入,同时它仍在导入,导致ImportError
。
Removing that import from __init__.py
will definitely solve the problem, but if you still need it there, you might be able to do something with activation/apps.py
instead.从
__init__.py
删除该导入肯定会解决问题,但是如果您仍然需要它,您可能可以使用activation/apps.py
做一些事情。
One thing I've learned so far is that 90% of the time ImportError
occurs due to circular imports, the other 10% is due to misspelling, or something like that.到目前为止,我了解到的一件事是
ImportError
发生的时间有 90% 是由于循环导入,另外 10% 是由于拼写错误或类似的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.