Я пытаюсь автоматизировать некоторые виды Django с помощью Celery, но не могу запустить рабочих из Celery из-за того, что Django не импортирует экземпляр Celery. Это очень похожая проблема на пост здесь:
Джанго сельдерей не находит модуль сельдерея
Обратите внимание, что я не использую virtenv и использую macos. Все пакеты были установлены на уровне корневого каталога (папка для проекта, который я использую для GitHub).
Я назвал проект iHand
и приложение app
из инструкции по установке Django.
app
был добавлен в INSTALLED_APPS
в settings.py
для регистрации приложения:
# Application definitions
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app', # Created app
]
Экземпляр app
создан в celery.py
:
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from celery import Celery
import os
# Create a whole instance of the Django project
inst = Celery('app')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'iHand.settings')
inst.config_from_object('django.conf:settings', namespace='CELERY')
inst.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
Создан пакет в __init__.py
, расположенном в iHand/iHand/
dir:
# Load app when Django starts
from __future__ import absolute_import, unicode_literals
from .celery import inst as celery_app
__all__ = ['celery_app']
Создан tasks.py
файл в iHand/app
:
# Import views and create tasks out of them
from celery import task
from app.views import (repositories, contributor,
teams, team_members, discussions)
# Get all of the repository names.
@task
repositories()
print('repositories executed')
# Get the contributors for each repository.
@task
contributor()
print('contributors executed')
# Get all of the teams in the organization.
@task
teams()
print('teams executed')
# Get all of the team members in each team.
@task
team_members()
print('team members executed')
# Get all discussions in each team.
@task
discussions()
print('discussions executed')
Я пытался удалить Celery и переустановить его с разных уровней каталога проекта, создав пакет в том же каталоге, что и tasks.py
, но это также не устранило проблему.
Вот вывод ошибки:
ModuleNotFoundError: No module named 'app'
И структура файла:
iHand
├── app
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tasks.py
│ ├── templates
│ │ └── app
│ │ ├── discussions.html
│ │ ├── repositories.html
│ │ ├── repository.html
│ │ ├── team_members.html
│ │ └── teams.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── iHand
│ ├── __init__.py
│ ├── celery.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py