我使用Django和芹菜遇到了ModuleNotFound错误。我有一个使用django rest框架的post端点,它运行芹菜任务来解析和存储json。我可以很好地为页面提供服务,但当我发布帖子时,会出现以下错误:
Exception Type: ModuleNotFoundError at /app/v1/results
Exception Value: No module named 'djcelery'
起初我认为可能有版本控制问题,所以我检查了我的包,但我是最新的,没有发现任何未解决的冲突。我确实在装载机上看到了DJ芹菜
初始化
。芹菜项目的py:
https://github.com/celery/celery/blob/master/celery/loaders/
init
.py
amqp (2.2.2)
billiard (3.5.0.3)
celery (4.1.0)
certifi (2018.1.18)
Django (2.0.3)
django-celery-results (1.0.1)
django-filter (1.1.0)
django-pyodbc-azure (2.0.3.0)
djangorestframework (3.7.7)
kombu (4.1.0)
Markdown (2.6.11)
mod-wsgi (4.6.2)
pip (9.0.1)
pyodbc (4.0.22)
pytz (2018.3)
setuptools (38.5.1)
vine (1.1.4)
wheel (0.30.0)
我的项目遵循Django first steps的核心
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#django-first-steps
:
Proj
- proj
- __init__.py
- celeryapp.py
- settings.py
- urls.py
- app
- models.py
- tasks.py
- views.py
- urls.py
- manage.py
对于我的所有代码,我都是从第一步教程开始的,但在遇到问题和查找帖子时做了一些更改,并试图找到相对于
NotFoundError
。我还添加了
from __future__ import absolute_import, unicode_literals
到项目中的所有文件。
在我的
settings.py
我有:
CELERY_BROKER_URL = 'amqp://user:pass@localhost:5672/proj_host'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = 'django-db'
在celeryapp中。py(由于导入错误和其他关于该问题的帖子而从celery.py重命名)我有:
from __future__ import absolute_import, unicode_literals
import os
from django import apps
from django.conf import settings
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
app.config_from_object(settings)
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}', format(self.request))
在我的
__init__.py
我有:
from __future__ import absolute_import, unicode_literals
from .celeryapp import app as celery_app
__all__ = ['celery_app']
最后
views.py
from __future__ import absolute_import, unicode_literals
import json
from app.tasks import process_json_result
from rest_framework.views import APIView
from rest_framework.response import Response
class AppResultView(APIView):
process_json_result.delay(json.dumps(request.data))
return Response({"success": True, "message": "Ok", "payload": ""})
和
tasks.py
from __future__ import absolute_import, unicode_literals
import json
import logging
from proj.celeryapp import app
from celery.utils.log import get_task_logger
from app.models import Result
logger = get_task_logger(__name__)
logger.setLevel(logging.DEBUG)
@app.task(name="app.tasks.process_json_result")
def process_json_result(result):
logger.info("Processing result")
py_result = json.loads(result)
从错误中回溯的时间越长:
ModuleNotFoundError at /app/v1/results
No module named 'djcelery'
Request Method: POST
Request URL: http://testdeployserver/app/v1/results
Django Version: 2.0.3
Python Executable:
Python Version: 3.6.4
Python Path: ['/var/www/python/proj', '/var/www/python/proj/proj', '/home/alex/anaconda3/envs/clean_django/lib/python36.zip', '/home/alex/anaconda3/envs/clean_django/lib/python3.6', '/home/alex/anaconda3/envs/clean_django/lib/python3.6/lib-dynload', '/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages']
Server time: Tue, 13 Mar 2018 23:00:10 +0000
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'rest_framework',
'django_celery_results']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/local.py" in _get_current_object
317. return object.__getattribute__(self, '__thing')
During handling of the above exception ('PromiseProxy' object has no attribute '__thing'), another exception occurred:
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/kombu/utils/objects.py" in __get__
42. return obj.__dict__[self.__name__]
During handling of the above exception ('data'), another exception occurred:
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/kombu/utils/objects.py" in __get__
42. return obj.__dict__[self.__name__]
During handling of the above exception ('loader'), another exception occurred:
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
494. response = self.handle_exception(exc)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
454. self.raise_uncaught_exception(exc)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
491. response = handler(request, *args, **kwargs)
File "/var/www/python/proj/app/views.py" in post
24. process_app_result.delay(json.dumps(request.data))
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/local.py" in __getattr__
146. return getattr(self._get_current_object(), name)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/local.py" in _get_current_object
319. return self.__evaluate__()
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/local.py" in __evaluate__
349. thing = Proxy._get_current_object(self)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/local.py" in _get_current_object
109. return loc(*self.__args, **self.__kwargs)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/app/base.py" in _task_from_fun
462. task.bind(self)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/app/task.py" in bind
320. setattr(cls, attr_name, conf[config_name])
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/utils/collections.py" in __getitem__
431. return getitem(k)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/utils/collections.py" in __getitem__
280. return mapping[_key]
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/collections/__init__.py" in __getitem__
987. if key in self.data:
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/kombu/utils/objects.py" in __get__
44. value = obj.__dict__[self.__name__] = self.__get(obj)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/app/base.py" in data
148. return self.callback()
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/app/base.py" in _finalize_pending_conf
911. conf = self._conf = self._load_config()
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/app/base.py" in _load_config
921. self.loader.config_from_object(self._config_source)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/kombu/utils/objects.py" in __get__
44. value = obj.__dict__[self.__name__] = self.__get(obj)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/app/base.py" in loader
1209. return get_loader_cls(self.loader_cls)(app=self)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/loaders/__init__.py" in get_loader_cls
21. return symbol_by_name(loader, LOADER_ALIASES, imp=import_from_cwd)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/kombu/utils/imports.py" in symbol_by_name
56. module = imp(module_name, package=package, **kwargs)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/site-packages/celery/utils/imports.py" in import_from_cwd
101. return imp(module, package=package)
File "/home/alex/anaconda3/envs/clean_django/lib/python3.6/importlib/__init__.py" in import_module
126. return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>" in _gcd_import
994. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load
971. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
941. <source code not available>
File "<frozen importlib._bootstrap>" in _call_with_frames_removed
219. <source code not available>
File "<frozen importlib._bootstrap>" in _gcd_import
994. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load
971. <source code not available>
File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
953. <source code not available>
Exception Type: ModuleNotFoundError at /app/v1/results
Exception Value: No module named 'djcelery'
从Apache/mod\wsgi anaconda环境路径中提取回溯上的路径看起来是正确的,我在anaconda virtualenvironment中看到了芹菜包文件,因此不确定哪些问题没有得到正确解决,或者我的任务路径设置是否不适合芹菜。