在gunicorn上部署flask应用程序,模块对象没有属性

时间:2015-06-09 11:12:55

标签: python flask gunicorn

我正在尝试将我的Flask应用程序部署到gunicorn,我收到了错误。

AttributeError: 'module' object has no attribute 'MyTopics'

当我只使用Flask服务器运行服务器时,一切正常:

python http_server/server.py

但是当我在gunicorn下运行它时,我得到了AttributeError

gunicorn -c http_server.config http_server.server:app

加载我的服务器应用程序如下:

import logging
import sys
import os
import json
import time
from functools import wraps

from flask import Flask, request, jsonify

from my_lda import MyTopics

import config

logger = logging.getLogger(__name__)
app = Flask(__name__)

class Server(object):
    def __init__(self, lda_model_path):
        self.lda_model_path = lda_model_path
        self.lda_model = self.load_to_lda_model()

    def load_to_lda_model(self, path=None):
        """
        load the lda model from the specific path
        """

        if path is None:
            path = self.lda_model_path

        logger.info("loading LDA model from %r" % path)
        lda_model = MyTopics.load(path)
        logger.info("loaded LDA model %s", lda_model)
        return lda_model

logging.basicConfig(format='%(processName)s %(process)s:-%(asctime)s : %(levelname)s : %(module)s:%(lineno)d : %(funcName)s(%(threadName)s) : %(message)s')
logging.root.setLevel(level=logging.INFO)
logging.info("running %s" % ' '.join(sys.argv))

if 'MODEL_PATH' in os.environ:
    path = os.environ['MODEL_PATH']
else:
    path = config.MODEL_FILE_PATH
app.config['server'] = Server(path)

if __name__ == '__main__':
    app.run()

调用加载是基于gensim.utils.SaveLoad,但我也尝试过标准酸洗,但没有区别。

到目前为止我的想法:gunicorn可以运行不同的Python(我是从同一个virtualenv发布它)吗? gunicorn是否可能看不到通过pip install -e .

安装的某些软件包

修改

添加项目结构:

  • HTTP_SERVER
    • __初始化__。PY
    • server.py
    • config.py
  • 数据
    • model.pkl
  • __初始化__。PY
  • my_lda.py
  • setup.py

1 个答案:

答案 0 :(得分:2)

所以这个问题与我挑选MyTopics对象的方式有关。 pickle can't import a module that exists?

中描述了更多详细信息