简体   繁体   English

使用 Elastic Beanstalk 部署 Flask 应用程序时遇到问题

[英]Trouble deploying Flask app using Elastic Beanstalk

I'm trying to deploy my flask app using Elastic Beanstalk.我正在尝试使用 Elastic Beanstalk 部署我的 Flask 应用程序。 When I deploy it and go to the site, I get an "Internal Server Error".当我部署它并转到站点时,我收到“内部服务器错误”。 I checked the logs and found I was getting "ModuleNotFoundErrors" but only with local imports.我检查了日志,发现我收到了“ModuleNotFoundErrors”,但仅限于本地导入。 My file structure looks like this:我的文件结构如下所示:

backend
   -alembic
   -libs
   -common
   -models
     -user.py
   -__init__.py
   -application.py
   -requirements.txt

So for example, I would get a `ModuleNotFoundError: No module named 'backend.models.user'.例如,我会得到一个`ModuleNotFoundError: No module named 'backend.models.user'。 Just out of curiosity, I changed the imports from the absolute path to relative path ('backend.models.user' -> '.models.user').出于好奇,我将导入从绝对路径更改为相对路径('backend.models.user' -> '.models.user')。 Now I'm getting the errors below:现在我收到以下错误:

[Sun Oct 04 15:26:10.439457 2020] [:error] [pid 23422] [remote 127.0.0.1:12] mod_wsgi (pid=23422): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Sun Oct 04 15:26:10.439510 2020] [:error] [pid 23422] [remote 127.0.0.1:12] mod_wsgi (pid=23422): Exception occurred processing WSGI script '/opt/python/current/app/application.py'.
[Sun Oct 04 15:26:10.439610 2020] [:error] [pid 23422] [remote 127.0.0.1:12] Traceback (most recent call last):
[Sun Oct 04 15:26:10.439639 2020] [:error] [pid 23422] [remote 127.0.0.1:12]   File "/opt/python/current/app/application.py", line 1, in <module>
[Sun Oct 04 15:26:10.439644 2020] [:error] [pid 23422] [remote 127.0.0.1:12]     from .config import BaseConfig
[Sun Oct 04 15:26:10.439659 2020] [:error] [pid 23422] [remote 127.0.0.1:12] ImportError: attempted relative import with no known parent package
[Sun Oct 04 15:26:11.442790 2020] [:error] [pid 23422] [remote 127.0.0.1:2576] mod_wsgi (pid=23422): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Sun Oct 04 15:26:11.442839 2020] [:error] [pid 23422] [remote 127.0.0.1:2576] mod_wsgi (pid=23422): Exception occurred processing WSGI script '/opt/python/current/app/application.py'.

I also tried taking out all imports to modules within the same directory while keeping the imports of built-in or installed modules.我还尝试在同一目录中取出所有模块的导入,同时保留内置或已安装模块的导入。 When I did that, the deployment worked with no Internal Server Error.当我这样做时,部署工作没有内部服务器错误。 Any ideas on why I'm having these issues?关于为什么我遇到这些问题的任何想法?

1st Update I went back and followed the AWS tutorial ( https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-flask.html ), but added a models package with test.py file to see if I could replicate my issue with the AWS code.第一次更新我回去遵循了 AWS 教程( https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-flask.html ),但添加了一个带有test.py文件的models包看看我是否可以用 AWS 代码复制我的问题。 In this example the file structure looks like this:在此示例中,文件结构如下所示:

~/eb-flask/
|-- virt
|-- models
   |-- test.py
|-- application.py
`-- requirements.txt

I then redeployed the code and got the same error:然后我重新部署了代码并得到了同样的错误:

[Sun Oct 04 23:31:28.271209 2020] [:error] [pid 3349] [remote 172.31.29.168:12] mod_wsgi (pid=3349): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Sun Oct 04 23:31:28.271276 2020] [:error] [pid 3349] [remote 172.31.29.168:12] mod_wsgi (pid=3349): Exception occurred processing WSGI script '/opt/python/current/app/application.py'.
[Sun Oct 04 23:31:28.271361 2020] [:error] [pid 3349] [remote 172.31.29.168:12] Traceback (most recent call last):
[Sun Oct 04 23:31:28.271386 2020] [:error] [pid 3349] [remote 172.31.29.168:12]   File "/opt/python/current/app/application.py", line 2, in <module>
[Sun Oct 04 23:31:28.271391 2020] [:error] [pid 3349] [remote 172.31.29.168:12]     from .models import test
[Sun Oct 04 23:31:28.271406 2020] [:error] [pid 3349] [remote 172.31.29.168:12] ImportError: attempted relative import with no known parent package

2nd Update Below is the code for my Flask app's application.py (minus a couple of the endpoints) and then the code for the AWS Flask tutorial's application.py (with the additional import I put in.) As stated in the first update, both my app and the tutorial's app had the same local import error.第二次更新下面是我的 Flask 应用程序的 application.py 的代码(减去几个端点),然后是 AWS Flask 教程的 application.py 的代码(以及我输入的额外导入。)如第一次更新中所述,我的应用程序和教程的应用程序都有相同的本地导入错误。

My application.py我的应用程序.py

from flask import Flask, render_template
from flask import request, session
from flask_cors import CORS
import os
from .common.api import api
from .journal_blueprint import journal_blueprint
from .manuscript_blueprint import manuscript_blueprint
from .user_blueprint import user_blueprint
from .models.decorators import requires_login
from .models.user import User
from .models.manuscript import Manuscript
from .models.journal import Journal
from .config_file import BaseConfig

application=Flask(__name__)

application.secret_key = BaseConfig.SECRET_KEY
application.config['SQLALCHEMY_DATABASE_URI'] = os.environ["DATABASE_URL"]

CORS(application)

application.register_blueprint(journal_blueprint, url_prefix='/journal')
application.register_blueprint(user_blueprint, url_prefix='/user')
application.register_blueprint(manuscript_blueprint, url_prefix='/manuscript')
application.register_blueprint(api, url_prefix="/api")


@application.route('/')
def home_template():
    return render_template('index.html')


@application.route('/login')
def login_template():
    return render_template('user/login.html')


@application.route('/register')
def register_template():
    return render_template('user/register.html')


@application.route('/search')
@requires_login
def search():
    return render_template("search.html")


if __name__ == '__main__':
    # application.run(debug=True)
    application.run()

AWS Tutorial application.py AWS 教程 application.py

from flask import Flask
from .models import test

# print a nice greeting.
def say_hello(username = "World"):
    return '<p>Hello %s!</p>\n' % username

# some bits of text for the page.
header_text = '''
    <html>\n<head> <title>EB Flask Test</title> </head>\n<body>'''
instructions = '''
    <p><em>Hint</em>: This is a RESTful web service! Append a username
    to the URL (for example: <code>/Thelonious</code>) to say hello to
    someone specific.</p>\n'''
home_link = '<p><a href="/">Back</a></p>\n'
footer_text = '</body>\n</html>'

# EB looks for an 'application' callable by default.
application = Flask(__name__)

# add a rule for the index page.
application.add_url_rule('/', 'index', (lambda: header_text +
    say_hello() + instructions + footer_text))

# add a rule when the page is accessed with a name appended to the site
# URL.
application.add_url_rule('/<username>', 'hello', (lambda username:
    header_text + say_hello(username) + home_link + footer_text))

# run the app.
if __name__ == "__main__":
    # Setting debug to True enables debug output. This line should be
    # removed before deploying a production app.
    application.debug = True
    application.run()

In your second attempt you are missing __init__.py .在您的第二次尝试中,您缺少__init__.py Thus it should be:因此它应该是:

~/eb-flask/
|-- virt
|-- models
   |-- __init__.py
   |-- test.py
|-- application.py
`-- requirements.txt

And in your application it should be (no dot):在您的应用程序中,它应该是(无点):

from models import test

Regarding my initial issue, I figured out how to fix it but I'm not sure how the issue came about in the first place.关于我最初的问题,我想出了如何解决它,但我不确定问题最初是如何产生的。 By looking at the sys.path , I found that the path that my app was looking for the modules in was starting at a directory one level higher than my application folder.通过查看sys.path ,我发现我的应用程序在其中查找模块的路径从比我的应用程序文件夹高一级的目录开始。 Not sure why that happened.不知道为什么会这样。 Maybe I did some weird copy/paste thing when I started the project 6 months ago.也许我在 6 个月前开始该项目时做了一些奇怪的复制/粘贴操作。 Anyway, when I would work on my machine, I would have to call my imports like this from .models.users import User instead of from models.user import User .无论如何,当我在我的机器上工作时,我必须像这样调用我的导入from .models.users import User而不是from models.user import User When I deployed the code on AWS, I'm assuming the path was set correctly at the application directory level, so my relative imports were incorrect.当我在 AWS 上部署代码时,我假设在应用程序目录级别正确设置了路径,因此我的相对导入不正确。 I had to change all my imports to fix this issue.我不得不更改我所有的导入来解决这个问题。 My IDE still highlights the imports as errors, but the code runs fine now, so I'll have to figure out why that is happening.我的 IDE 仍然将导入突出显示为错误,但代码现在运行良好,所以我必须弄清楚为什么会发生这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 AWS Elastic Beanstalk 中部署 Flask 应用程序 - Deploying Flask App in AWS Elastic Beanstalk 在Elastic Beanstalk上使用Docker部署Flask应用程序:资源创建取消错误 - Deploying Flask app with Docker on Elastic Beanstalk: resource creation cancelled error 通过与S3交互的Elastic Beanstalk部署Flask应用 - Deploying Flask app to via Elastic Beanstalk which interacts with S3 使用 CLI 将 flask 应用程序部署到弹性 beanstalk 时出现 502 错误 - 502 error deploying flask application to elastic beanstalk using CLI 将带有熊猫的Flask应用程序部署到Elastic Beanstalk - Deploying Flask Application with pandas to Elastic Beanstalk 使用 awsebcli 部署 Elastic Beanstalk 应用程序时出现“操作被拒绝”错误 - 'Operation Denied' Error when deploying an Elastic Beanstalk app using awsebcli 如何使用应用程序工厂将Flask应用程序部署到AWS Elastic beantalk - How to deploy Flask app to AWS Elastic beanstalk using an application factory ModuleNotFoundError:在 AWS Elastic Beanstalk 上部署 Flask 应用程序时没有名为“应用程序”的模块 - ModuleNotFoundError: No module named 'application' when deploying Flask app on AWS Elastic Beanstalk 如何在部署 Flask 应用程序时修复此 Elastic Beanstalk 错误:在您的系统上找不到 Python 3.7.0 - How do I fix this Elastic Beanstalk error when deploying a Flask app: Python 3.7.0 was not found on your system 在 Elastic Beanstalk 上部署 Django 应用程序时出错 - Error Deploying Django App on Elastic Beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM