Skip to main content
 首页 » 编程设计

python之Django celery 导入错误 : no module named celery when using gunicorn bind

2025年02月15日25lovecherry

我一直在到处寻找这个问题的答案。我正在 Ubuntu 16.04( digital ocean )上为我的 Django 网站设置服务器,我的 Django 网站需要使用 celery 来完成一些周期性任务。

它适用于我的开发环境。运行 python manage.py celery beat 和 python manage.py celery worker 工作得很好。它也全部安装在 virtualenv 中。

这是我的文件:

# __init__.py

from __future__ import absolute_import 
from .celery_tasks import app as celery_app  # noqa 

#celery_tasks.py

from __future__ import absolute_import 
 
import os 
from celery import Celery 
 
# set the default Django settings module for the 'celery' program. 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') 
 
from django.conf import settings  # noqa 
 
app = Celery('myproject') 
 
app.config_from_object('django.conf:settings') 
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) 
 
 
@app.task(bind=True) 
def debug_task(self): 
    print('Request: {0!r}'.format(self.request)) 

这是一直在发生的错误:

# gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

File "/root/myproject/myproject/__init__.py", line 2, in <module> 
from .celery_tasks import app as celery_app  # noqa 
File "/root/myproject/myproject/celery_tasks.py", line 4, in <module> 
from celery import Celery 
ImportError: No module named celery 
[2017-08-13 07:29:36 +0000] [5463] [INFO] Worker exiting (pid: 5463) 
[2017-08-13 07:29:36 +0000] [5458] [INFO] Shutting down: Master 
[2017-08-13 07:29:36 +0000] [5458] [INFO] Reason: Worker failed to boot. 

还有一些看起来不太相关的追溯。

非常感谢任何帮助。我想我错过了一些简单的东西,但我已经为此苦苦挣扎了几个小时。

请您参考如下方法:

错误说不是创建 celery 。 所以将 celery 放在你的 requirements.txt 文件中,当你部署它时安装 Celery。 或者在你的服务器上做:

pip install celery 

或在您的 requirements.txt 中插入 celery 并执行:

pip install -r requirements.txt