ModuleNotFoundError: No module named 'celery'

来源:12-9 Nginx Tornado部署Django应用

helloap

2019-07-13

我在学习第12-9章 Tornado部署的时候,执行 python TornadoServer.py —port=6001的时候会报错如下:
图片描述
这是celery_app.py的代码:

import os
from celery import Celery

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

app = Celery("zanhu")

# Using a string here means the worker doesn'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()

这是TornadoServer.py的代码:

import os
import sys
from tornado.options import options, define
from django.core.wsgi import get_wsgi_application
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi

# Django Application加入查找路径中
app_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir))
sys.path.append(os.path.join(app_path, 'zanhu'))  # ../zanhu/zanhu
define("port", default=6000, type=int, help="run on the given port")


def main():
    tornado.options.parse_command_line()
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
    wsgi_app = tornado.wsgi.WSGIContainer(get_wsgi_application())
    http_server = tornado.httpserver.HTTPServer(wsgi_app, xheaders=True)  # xheaders=True是啥意思?
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    main()

图片描述

启动tornado的时候好像不会从pipenv创建的虚拟环境目录去寻找module.当我把全局里的uninstall tornado 之后,只有在项目的虚拟环境里install tornado,连tornado也找不到。
所有执行文件里的module都会找不到。难道所有项目依赖都要全局install一遍?
ps:项目本身是能正常启动的

写回答

1回答

Jack

2019-07-13

本小节部署讲解使用的pipenv虚拟环境中的python,与全局(系统上的python)的无关。你碰到的,

  1. 要么是python虚拟环境有问题

    pip uninstall celery # 先把全局(系统上的python)celery卸载掉

    cd 项目路径

    pipenv shell # 进入虚拟环境

    which python或whereis python 看下是不是使用了虚拟环境的python

    pip list | grep celery # 虚拟环境中安装了celery没,(用celery==4.2.1)

  2. 要么是路径设置有问题找不到module

    我是在/root/zanhu下,进入pipenv shell,然后python TornadoServer.py —port=6001

    在TornadoServer.py里面 “sys.path.append(os.path.join(app_path, 'zanhu'))  # ../zanhu/zanhu
    ”,你可以print看下你的app_path是不是Desktop/backend/python/django/zanhu

1
0

Django高级实战 开发企业级问答网站

融合Django高级用法/算法/设计模式/TestCase测试/云计算打造项目

908 学习 · 757 问题

查看课程