导入错误:没有名为 'tasks' 的模块



我正在尝试让 Celery 使用 django 来设置计划任务。我尝试查看带有芹菜的第一步和带有 Django 教程的第一步,但两者都不适合我。这是我的项目布局和相关文件:

蟒蛇 3.5.1

姜戈 1.10

芹菜 4.0.2

兔子MQ 3.6.6

一次性密码 19.2

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

我的网站/__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']

民意调查/芹菜.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))

投票/任务.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:usersusernameappdatalocalprogramspythonpython35librunpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "c:usersusernameappdatalocalprogramspythonpython35librunpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:UsersusernameAppDataLocalProgramsPythonPython35Scriptscelery.exe__main__.py", line 9, in <module>
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagescelery__main__.py", line 14, in main
    _main()
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagescelerybincelery.py", line 326, in main
    cmd.execute_from_commandline(argv)
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagescelerybincelery.py", line 488, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagescelerybinbase.py", line 279, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagescelerybinbase.py", line 481, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagescelerybinbase.py", line 503, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagesceleryapputils.py", line 355, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagescelerybinbase.py", line 506, in symbol_by_name
    return imports.symbol_by_name(name, imp=imp)
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packageskombuutilsimports.py", line 56, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "c:usersusernameappdatalocalprogramspythonpython35libsite-packagesceleryutilsimports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "c:usersusernameappdatalocalprogramspythonpython35libimportlib__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'

问题出在这一行:

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

执行此命令的位置错误。

相关内容

  • 没有找到相关文章

最新更新