web-dev-qa-db-ja.com

ImportError:「tasks」という名前のモジュールがありません

CeleryをDjangoで動作させて、スケジュールされたタスクを設定しようとしています。 Celeryを使用した最初のステップ および 最初のステップ)を調べてみました。ステップw/Django チュートリアルで、どちらも機能していません。関連ファイルを含むプロジェクトレイアウトは次のとおりです。

Python 3.5.1

Django 1.10

セロリ4.0.2

RabbitMQ 3.6.6

OTP 19.2

mysite/ (project name)
    polls/ (myapp)
        tasks
        ...
    mysite/
        __init__
        celery
        settings
        ...
    manage
    ...

mysite/__ 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 import app as celery_app

__all__ = ['celery_app']

polls/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', 'mysite.settings')

app = Celery('mysite', backend='amqp', broker='amqp://guest@localhost//',include=['polls.tasks'])

# 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))

polls/tasks.py:

# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task


@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)

私は実行しようとしました:

\mysite> celery -A tasks worker --loglevel=info

そしてその結果は次のとおりです。

Traceback (most recent call last): File "c:\users\username\appdata\local\programs\python\python35\lib\runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\username\appdata\local\programs\python\python35\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\username\AppData\Local\Programs\Python\Python35\Scripts\celery.exe\__main__.py", line 9, in <module>
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\__main__.py", line 14, in main
    _main()
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\celery.py", line 326, in main
    cmd.execute_from_commandline(argv)
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\celery.py", line 488, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\base.py", line 279, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\base.py", line 481, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\base.py", line 503, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\app\utils.py", line 355, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\bin\base.py", line 506, in symbol_by_name
    return imports.symbol_by_name(name, imp=imp)
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\kombu\utils\imports.py", line 56, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "c:\users\username\appdata\local\programs\python\python35\lib\site-packages\celery\utils\imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "c:\users\username\appdata\local\programs\python\python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'tasks'
7
user2361174

問題は次の行にあります。

\mysite> celery -A tasks worker --loglevel=info

外側のmysiteフォルダーにtasks.pyファイルがありません:

必要なのは

\mysite> celery -A polls/tasks worker --loglevel=info

または:

\mysite> celery worker --app=mysite --loglevel=info
\mysite> celery worker -A proj -loglevel=info

このコマンドを実行している場所が間違っています。

14
Prakhar Trivedi