Unsuccessful Flask app deployment on EC2 instance


I am trying to deploy a flask app on an EC2 instance but running into the following error:
enter image description here

The directory structure of my app is as follows:


Contents of ImgClassApp.wsgi are as follows:

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

And finally, contents of app.py are as follows:

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__)

def index():
    return render_template("index.html")

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

    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))

        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)
    return render_template("upload.html", predictions=preds, display_image=f.filename) 

if __name__ == "__main__":


The best way to address the issue would be to reorganize directory structure a bit.
In fact if you just move the file ImgClassApp.wsgi to the upper directory, this should work and it will be able to import from directory ImgClassApp, that is just underneath.

No need to mess up with the Python path. Python already includes your application working directory in its search path.

Answered By – Anonymous

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published