gpt4 book ai didi

python - 导入错误: No module named celery

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:08 25 4
gpt4 key购买 nike

我正在尝试按照 this tutorial 使用我的 Django 项目运行定期任务.

但是,当尝试使用以下命令运行 C​​elery Worker 时:

celery -A myproject worker -l info

我收到以下错误:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named celery

我怀疑这可能是因为我的设置文件(local.py 和 production.py)不符合标准 Django 文件结构,它们嵌套在下一个文件夹中。

├── myproject
│   ├── myproject
│   │   ├── __init__.py
│   │   ├── settings
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── celery.py
│   │   │   ├── local.py
│   │   │   ├── production.py
│   ├── manage.py

__init__.py

# myproject/myproject/settings/__init__.py

from __future__ import absolute_import, unicode_literals

from .base import *

try:
from .local import *
live = False
except:
live = True

if live:
from .production import *

from .celery import app as celery_app

celery.py

# myproject/myproject/settings/celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.local')

app = Celery('myproject')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))



from celery.schedules import crontab
app.conf.beat_schedule = {
'add-every-minute-contrab': {
'task': 'fetch_news',
'schedule': crontab()
}
}

Celery 肯定已安装。

我查看了很多有关此错误的其他线程,但没有一个有帮助。任何建议将不胜感激。

谢谢

最佳答案

通过将以下内容添加到上面目录中的 __init__.py 来修复。

# myproject/myproject/__init__.py

from __future__ import absolute_import

from myproject.settings.celery import app as celery_app

关于python - 导入错误: No module named celery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48957246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com