Django+Celery运行报错:No module named '<project_name>' 求助
Celery启动报错ModuleNotFoundError求助
调试这个问题已经第二天了,只能来社区求助。几个月前用cookiecutter-django生成了项目,结构如下:
Project name config/settings/... src/ app_name/ __init__.py manage.py
最初在config目录下创建celery.py时因重名报错,尝试absolute_import后参考cookiecutter-django的命名改成了celery_app.py,代码如下:
celery_app.py
import os from celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.base") app = Celery("my_awesome_project") #I tried this with 10 different names, doesn't make any difference # Using a string here means the worker doesn'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()
config目录下的__init__.py
#from __future__ import absolute_import, unicode_literals # This will make sure the app is always imported when # Django starts so that shared_task will use this app. from .celery_app import app as celery_app __all__ = ('celery_app',)
运行Celery命令时出现如下报错:
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked ModuleNotFoundError: No module named '<project_name>'
已知Python3.7与Celery存在兼容问题需降级importlib-metadata至4.13.0,我使用Python3.9.6,已尝试降级及安装最新版本的importlib-metadata,也在项目根目录尝试了多种Celery命令,如celery -A config.celery_app worker -l info、celery -A config.celery_app:app worker -l DEBUG等,且redis服务正常运行,仍无法解决问题,恳请有类似经验的开发者提供帮助。
内容的提问来源于stack exchange,提问作者Vinko Oakniiv
相关产品推荐
相关产品推荐

