繁体   English   中英

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

[英]Trouble deploying Flask app using Elastic Beanstalk

我正在尝试使用 Elastic Beanstalk 部署我的 Flask 应用程序。 当我部署它并转到站点时,我收到“内部服务器错误”。 我检查了日志,发现我收到了“ModuleNotFoundErrors”,但仅限于本地导入。 我的文件结构如下所示:

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

例如,我会得到一个`ModuleNotFoundError: No module named 'backend.models.user'。 出于好奇,我将导入从绝对路径更改为相对路径('backend.models.user' -> '.models.user')。 现在我收到以下错误:

[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'.

我还尝试在同一目录中取出所有模块的导入,同时保留内置或已安装模块的导入。 当我这样做时,部署工作没有内部服务器错误。 关于为什么我遇到这些问题的任何想法?

第一次更新我回去遵循了 AWS 教程( https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-flask.html ),但添加了一个带有test.py文件的models包看看我是否可以用 AWS 代码复制我的问题。 在此示例中,文件结构如下所示:

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

然后我重新部署了代码并得到了同样的错误:

[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

第二次更新下面是我的 Flask 应用程序的 application.py 的代码(减去几个端点),然后是 AWS Flask 教程的 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 教程 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()

在您的第二次尝试中,您缺少__init__.py 因此它应该是:

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

在您的应用程序中,它应该是(无点):

from models import test

关于我最初的问题,我想出了如何解决它,但我不确定问题最初是如何产生的。 通过查看sys.path ,我发现我的应用程序在其中查找模块的路径从比我的应用程序文件夹高一级的目录开始。 不知道为什么会这样。 也许我在 6 个月前开始该项目时做了一些奇怪的复制/粘贴操作。 无论如何,当我在我的机器上工作时,我必须像这样调用我的导入from .models.users import User而不是from models.user import User 当我在 AWS 上部署代码时,我假设在应用程序目录级别正确设置了路径,因此我的相对导入不正确。 我不得不更改我所有的导入来解决这个问题。 我的 IDE 仍然将导入突出显示为错误,但代码现在运行良好,所以我必须弄清楚为什么会发生这种情况。

暂无
暂无

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

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