[英]ModuleNotFoundError: No module named 'rest_framework' when running celery -A backend worker -l info
I'm trying to run my tasks and I'm currently using Celery for this alongside Django and DjangoRestFramework我正在尝试运行我的任务,我目前正在使用 Celery 以及 Django 和 DjangoRestFramework
This is how I've set things up, root directory is also called backend
这就是我设置的方式,根目录也称为
backend
.
├── backend
│ ├── asgi.py
│ ├── celery.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── db.sqlite3
├── manage.py
└── save_to_db
├── admin.py
├── apps.py
├── dump.rdb
├── migrations
│ ├── 0001_initial.py
│ └── 0002_auto_20210728_0251.py
├── models.py
├── serializers.py
├── tasks.py
├── tests.py
├── urls.py
└── views.py
So I have all my configuration for Celery in celery.py
which I followed from this on how to set up Celery for Django https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html This is how my config looks like So I have all my configuration for Celery in
celery.py
which I followed from this on how to set up Celery for Django https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html这就是我的配置的样子
import os
from celery import Celery
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
app = Celery('backend')
# 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 apps.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
I decided to run this command to get Celery started celery -A backend worker -l info
我决定运行这个命令来启动
celery -A backend worker -l info
But this gives me this error但这给了我这个错误
Traceback (most recent call last):
File "/usr/local/bin/celery", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.9/site-packages/celery/__main__.py", line 15, in main
sys.exit(_main())
File "/usr/local/lib/python3.9/site-packages/celery/bin/celery.py", line 213, in main
return celery(auto_envvar_prefix="CELERY")
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.9/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/celery/bin/base.py", line 133, in caller
return f(ctx, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/celery/bin/worker.py", line 338, in worker
worker = app.Worker(
File "/usr/local/lib/python3.9/site-packages/celery/worker/worker.py", line 94, in __init__
self.app.loader.init_worker()
File "/usr/local/lib/python3.9/site-packages/celery/loaders/base.py", line 111, in init_worker
self.import_default_modules()
File "/usr/local/lib/python3.9/site-packages/celery/loaders/base.py", line 105, in import_default_modules
raise response
File "/usr/local/lib/python3.9/site-packages/celery/utils/dispatch/signal.py", line 276, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/local/lib/python3.9/site-packages/celery/fixups/django.py", line 82, in on_import_modules
self.worker_fixup.validate_models()
File "/usr/local/lib/python3.9/site-packages/celery/fixups/django.py", line 120, in validate_models
self.django_setup()
File "/usr/local/lib/python3.9/site-packages/celery/fixups/django.py", line 116, in django_setup
django.setup()
File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 224, in create
import_module(entry)
File "/usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
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 'rest_framework'
I have installed DjangoRestFramework via Poetry and it shows up in my pyproject.toml
and I've include rest_framework
in my INSTALLED_APPS
in settings我已经通过 Poetry 安装了 DjangoRestFramework,它显示在我的
pyproject.toml
中,并且我在设置中的INSTALLED_APPS
中包含rest_framework
Installed apps and some other config for celery in my settings.py
在我的
settings.py
中为 celery 安装了应用程序和其他一些配置
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'save_to_db'
]
# Celery Configuration Options
CELERY_TIMEZONE = "US/Eastern"
CELERY_TASK_TRACK_STARTED = True
# CELERY STUFF
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
Is your project running on virtual environment?您的项目是否在虚拟环境中运行? If so run celery within virtual environment.
如果是这样,在虚拟环境中运行 celery。
I ran in the same issus, for me i installed djangorestframework after building the container thats why i didnt recongnize it after a run the container again.我在同一个问题中运行,对我来说,我在构建容器后安装了 djangorestframework,这就是为什么我在再次运行容器后没有重新识别它的原因。 so what i did is i added the djangorestframework to my requirments.txt and rebuild the image from scratch
所以我所做的就是将 djangorestframework 添加到我的 requirments.txt 并从头开始重建图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.