没有带有 uWSGI 和 Python3 的模块名称 celery

No module name celery with uWSGI and Python3

Traceback (most recent call last):
  File "./fb_archive/__init__.py", line 5, in <module>
    from .celery import app as celery_app
  File "./fb_archive/celery.py", line 5, in <module>
    from celery import Celery
ImportError: No module named 'celery'
unable to load app 0 (mountpoint='') (callable not found or import error)

uWGSI 说 No module name celery。 它在没有 uWGSI 的情况下运行良好。 我使用 python 3.5 和 virtualenv.

我用 python 2.7 和 uWGSI 测试,它可以加载芹菜。 如何使用 python 3.x 加载芹菜?

这是我的celery.py。

from __future__ import absolute_import

import os

from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fb_archive.settings')

app = Celery('fb_archive')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)

app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.cache:CacheBackend',
)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

这是我的 init.py.

from __future__ import absolute_import

# 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

您可能为 Python2.7 安装了 celery,但没有为 Python3 安装。您可以尝试使用 pip3 install celery.

为 Python3 安装它

也有可能您确实在您的正常环境中为Python3安装了celery,但它没有安装在您的virtualenv中。

将 uwsgi 用于 python3.X

如果你想 运行 django in python3.X (virtualenv) with uwsgi,那么 uwsgi 必须安装在 python3.X virtualenv 中,我猜你是 运行ning python2.7 没有 virtualenv 的 uwsgi。您需要删除安装的 uwsgi,然后在 python3.X 的 virtualenv 中重新安装 uwsgi,然后当您从 virtualenv 中 运行 uwsgi 时它将包含 celery。

按照以下步骤操作:

  • 删除 uwsgi sudo pip uninstall uwsgi
  • 进入项目目录
  • 激活你的虚拟环境 source venv/bin/activate
  • 在激活的 virtualenv 中全新安装 uwsgipip install uwsgi
  • 现在 运行 你的 django 项目使用 ini 文件 uwsgi --ini /etc/uwsgi/sites/project.ini
  • 如果您对某些文件的任何权限被拒绝,只需将其更改 sudo chown <user> /path/to/file 或只执行 sudo。