Celery worker ImportError: No module named 'project'

Celery worker ImportError: No module named 'project'

当我尝试启动 worker 时遇到问题:
ImportError: 没有名为 'project'

的模块

回溯(最近调用最后):

  File "/usr/local/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/dist-packages/celery/__main__.py", line 16, in main
    _main()
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/celery.py", line 322, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/celery.py", line 496, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 273, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 479, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 501, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/local/lib/python3.5/dist-packages/celery/app/utils.py", line 359, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 504, in symbol_by_name
    return imports.symbol_by_name(name, imp=imp)
  File "/usr/local/lib/python3.5/dist-packages/kombu/utils/imports.py", line 56, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/celery/utils/imports.py", line 104, in import_from_cwd
    return imp(module, package=package)
  File "/usr/lib/python3.5/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 'project'

我在ubuntu16.04中使用了celery和redis。以下是我的处理步骤:

首先安装celery和redis:

pip install -U celery[redis] 

sudo apt-get install redis-server

当我使用:

ps aux|grep redis

我可以看到 redis 正在列出端口 6379

第二个 project.setting 添加:

#Broker 
BROKER_URL = 'redis://127.0.0.1:6379/0'
BROKER_TRANSPORT = 'redis' 

项目文件夹下的第三个构建celery.py,project.setting也在这个文件夹中:

from __future__ import absolute_import
import os
import django
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django.setup()
app = Celery('project')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

Forth build tasks.py under app will use it

from project.celery import app


from random import Random
from django.core.mail import send_mail

from users.models import EmailVerifyRecord
from project.settings import EMAIL_FROM


@app.task
def random_str(randomlength=8):
    str = ''
    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    length = len(chars) - 1
    random = Random()
    for i in range(randomlength):
        str += chars[random.randint(0, length)]
    return str

完成所有这些步骤后,在 celery.py I 运行:

的相同路径
celery -A project worker -l debug

然后我发现了问题。

有朋友可以帮忙吗?

非常感谢!

这只是因为你不在你的根项目中

导航到您的根项目,与 manage.py

相同的目录
cd path/to/project

和运行再次执行命令。

celery -A project worker -l debug