EC2实例上的Flask应用部署失败

2024-06-10 22:16:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在EC2实例上部署flask应用程序,但遇到以下错误: enter image description here

我的应用程序的目录结构如下:

/var/www/html/ImgClassApp/
        |--ImgClassApp.wsgi
        |--1000_imagenet_classes_file.txt
        |--app.py
        |--app_helper.py
        |--requirements.txt
        |--/static
           |--/css
           |--/uploads
        |--/templates
           |--index.html
           |--layout.html
           |--upload.html

ImgClassApp.wsgi的内容如下:

import sys
sys.path.insert(0, '/var/www/html/ImgClassApp')
from ImgClassApp import app as application

最后,app.py的内容如下:

from flask import Flask, request, render_template
from werkzeug.utils import secure_filename
import os
from app_helper import *

# Define a flask app
app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")


@app.route('/uploader', methods = ['POST'])
def upload_file():
    predictions=""

    if request.method == 'POST':
        f = request.files['file']

        # Save the file to ./uploads
        basepath = os.path.dirname(__file__)
        file_path = os.path.join(basepath, 'static','uploads', secure_filename(f.filename))
        f.save(file_path)

        predictions = get_classes(file_path)
        pred_strings = []
        for _,pred_class,pred_prob in predictions:
            pred_strings.append(str(pred_class).strip()+" : "+str(round(pred_prob,5)).strip())
        preds = ", ".join(pred_strings)
        print("preds:::",preds)
    return render_template("upload.html", predictions=preds, display_image=f.filename) 


if __name__ == "__main__":
    app.run(debug=True)

Tags: pathfrompyimportappflaskindexhtml
1条回答
网友
1楼 · 发布于 2024-06-10 22:16:58

解决这个问题的最好办法是稍微重新组织一下目录结构。 事实上,如果您只是将文件ImgClassApp.wsgi移动到上面的目录,这应该可以工作,并且它将能够从下面的目录ImgClassApp导入

不需要弄乱Python路径。Python已经在其搜索路径中包含了您的应用程序工作目录

相关问题 更多 >