gpt4 book ai didi

python - 尝试将 Flask 应用程序部署到 AWS Elastic Beanstalk 时,我不断收到 "ModuleNotFoundError: No module named ' 应用程序的错误消息

转载 作者:行者123 更新时间:2023-12-05 03:47:11 25 4
gpt4 key购买 nike

我正在尝试将 Flask 网络应用程序部署到 AWS Elastic Beanstalk,但始终失败。当我启动环境时,它向我抛出 502 错误,并且运行状况设置为 Severe 并出现以下错误:

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.

我检查了日志,看来问题可能归结为两个(可能相关的)错误:

  1. ModuleNotFoundError: 没有名为“app”的模块
  2. 无法在“应用程序”中找到属性“应用程序”。

作为引用,这是我的应用程序的结构:

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

app.py 模块如下所示:

from app import create_app

application = app = create_app()

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

app/__init__.py 模块如下所示:

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

在 AWS EB 环境中(在 64 位 Amazon Linux 2/3.1.3 上运行的 Python 3.7),我已将 WSGIPath 配置变量设置为 app:application。我是根据建议(来自 https://forums.aws.amazon.com/thread.jspa?threadID=321677 )这样做的:

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.

根据其他帖子的推荐,我什至(实际上是好几次)将 app.py 更改为 application.py(同时还编辑了 WSGIPath 变量),其中没有成功。

我已经在本地测试了应用程序(使用 flask runpython app.py),它在我的本地机器上运行正常,但是当我部署它时,我收到错误。

我似乎在无休止地寻找解决方案,我相信我已经正确地实现了部署和在下面找到的其他修复(以及通过我的调试过程进行的许多其他修复),尽管似乎都没有用:

如果有人能帮助我,我将不胜感激!

最佳答案

不知何故,在过去尝试修复部署的过程中,我设法做了足够多的工作,使它接近成功。我重命名了主文件 application.py(来自 app.py),这似乎解决了我的问题。

我还必须为该站点的索引值 ('/') 添加路由,即使有蓝图也是如此。

关于python - 尝试将 Flask 应用程序部署到 AWS Elastic Beanstalk 时,我不断收到 "ModuleNotFoundError: No module named ' 应用程序的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64977762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com