简体   繁体   中英

When trying to deploy a Flask app to AWS Elastic Beanstalk, I keep getting the "ModuleNotFoundError: No module named 'app'" error message

I am trying to deploy a Flask web app to AWS Elastic Beanstalk, and it consistently fails. When I launch the environment, it throws me a 502 error, and the health is set to Severe with the following errors:

100.0 % of the requests are failing with HTTP 5xx. ELB processes are not healthy on all instances. ELB health is failing or not available for all instances. Impaired services on all instances.

I have checked the logs, and it seems that the problem likely boils down to two (probably related) errors:

  1. ModuleNotFoundError: No module named 'app'
  2. Failed to find attribute 'application' in 'app'.

For reference, here is the structure of my app:

myApp/
   app/
      admin/
         __init__.py
         ...
      ms/
         __init__.py
         ...
      wf/
         __init__.py
         ...
      static/
         ...
      templates/
         ...
      __init__.py
      models.py
   app.py
   config.py
   requirements.txt

The app.py module looks like this:

from app import create_app

application = app = create_app()

if __name__ == "__main__":
    application.run()

The app/__init__.py module looks like:

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_cors import CORS, cross_origin
from flask_bootstrap import Bootstrap
from flask_login import LoginManager
from flask_moment import Moment
import os

db = SQLAlchemy()
migrate = Migrate()
bootstrap = Bootstrap()
login = LoginManager()
login.login_view = 'admin.login'
login.login_message = 'Please log in to access this page.'
moment = Moment()

def create_app(config_class=Config):
    application = app = Flask(__name__)
    app.config.from_object(config_class)
    CORS(app)

    db.init_app(app)
    migrate.init_app(app, db, render_as_batch=True)
    bootstrap.init_app(app)
    login.init_app(app)
    moment.init_app(app)

    from app.ms import bp as ms_bp
    app.register_blueprint(ms_bp, url_prefix='/ms', template_folder='templates', static_folder='static')

    from app.wf import bp as wf_bp
    app.register_blueprint(wf_bp, url_prefix='/wf', template_folder='templates', static_folder='static')

    from app.admin import bp as admin_bp
    app.register_blueprint(admin_bp, url_prefix='/admin', template_folder='templates', static_folder='static')

    return app

from app import models

In the AWS EB environment (Python 3.7 running on 64bit Amazon Linux 2/3.1.3), I have set the WSGIPath configuration variable to app:application . I did this based on a recommendation (from https://forums.aws.amazon.com/thread.jspa?threadID=321677 ) that says:

For Linux 2 - python 3.7, this translates to "WSGIPath: myapplication:myapp" where myapplication.py is the module name and "app" is the variable name set to be the flask application object.

As recommended by other posts, I have even (several times, actually) changed app.py to application.py (while also editing the WSGIPath variable), with no success.

I have tested the app locally (both with flask run and python app.py ), and it runs properly on my local machine, but when I deploy it, I get errors.

I have searched seemingly endlessly for solutions, and I believe I have properly implemented the deployment and other fixes found at the following (and many others through my debugging process), though none seem to work:

If anyone can help me out, it would be MUCH appreciated!

Somehow in the process of trying to fix the deployment in the past, I managed to do enough to get it nearly there. I renamed the main file application.py (from app.py ), and this seemed to solve my problem.

I also had to add a route for the index value ('/') for the site, even with the blueprints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM