简体   繁体   English

将简单的 web 应用程序部署到 Elastic Beanstalk 时没有名为“应用程序”的模块出错

[英]No module named 'application' Error while deploying simple web app to Elastic Beanstalk

I am deploying a web app to elastic beanstalk using this tutorial and the same 'application.py' file they have: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-flask.html#python-flask-setup-venv我正在使用本教程和它们具有的相同“application.py”文件将 web 应用程序部署到弹性 beanstalk: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-flask。 html#python-flask-setup-venv

I get a 502 error when going to the site, and degraded/severe health on the environment.我在访问该站点时收到 502 错误,并且环境运行状况下降/严重。 When I check the logs, I see this (which I assume is the root of the problem):当我检查日志时,我看到了这个(我认为这是问题的根源):

Jun 19 22:05:18 ip-172-31-15-237 web: File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
Jun 19 22:05:18 ip-172-31-15-237 web: return _bootstrap._gcd_import(name[level:], package, level)
Jun 19 22:05:18 ip-172-31-15-237 web: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
Jun 19 22:05:18 ip-172-31-15-237 web: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
Jun 19 22:05:18 ip-172-31-15-237 web: File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
Jun 19 22:05:18 ip-172-31-15-237 web: ModuleNotFoundError: No module named 'application'

Here is my application.py file:这是我的 application.py 文件:


from flask import Flask

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

And here is my requirements.txt file:这是我的 requirements.txt 文件:

click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
numpy==1.16.3
pandas==0.24.2
python-dateutil==2.8.1
pytz==2020.1
six==1.15.0
Werkzeug==1.0.1

The zipped folder that I upload to elastic beanstalk consists of just these two files.我上传到 elastic beanstalk 的压缩文件夹仅包含这两个文件。 I did have a virtual environment in there too, but the tutorial says you don't need it so I got rid of it.我在那里也有一个虚拟环境,但是教程说你不需要它所以我把它去掉了。

Also I am running Python 3.7.1 so I have pip3.我也在运行 Python 3.7.1,所以我有 pip3。 And I should note that the web app works when I just run the python code.我应该注意到,当我运行 python 代码时,web 应用程序可以正常工作。

A possible reason is the use of Amazon Linux 2 environment, instead of Amazon Linux 1 .一个可能的原因是使用了Amazon Linux 2环境,而不是Amazon Linux 1

The list of python environments and their linux distributions is here . python 环境及其 linux 分布列表在这里

From the link you provided:从您提供的链接:

In this tutorial we use Python 3.6 and the corresponding Elastic Beanstalk platform version.在本教程中,我们使用 Python 3.6和相应的 Elastic Beanstalk 平台版本。

The Python 3.6 is supported in Amazon Linux 1 environment, while you are using Python 3.7 which is for Amazon Linux 2 environment. The Python 3.6 is supported in Amazon Linux 1 environment, while you are using Python 3.7 which is for Amazon Linux 2 environment.

There are many differences between AL1 and AL2, which make them incompatible . AL1 和 AL2 之间存在许多差异,这使得它们不兼容

I had the same issue as you.我和你有同样的问题。 Downgrading to AL1 does solve this but brings more issues.降级到 AL1 确实解决了这个问题,但带来了更多问题。

I found that you can make your app compatible by adding the following line to the end of the file.我发现您可以通过在文件末尾添加以下行来使您的应用程序兼容。

app = application

Alternatifly you can do this when you define the flask instance:或者,您可以在定义 flask 实例时执行此操作:

application = app = Flask(__name__) do not forget to import both into application.py if needed. application = app = Flask(__name__)如果需要,不要忘记将两者都导入到 application.py 中。

This works because aws needs the Flask instance to be called application but gunicorn needs the Flask instance to be called app so this way you make them both happy.这是因为 aws 需要将 Flask 实例称为应用程序,但 gunicorn 需要将 Flask 实例称为应用程序,所以这样你就可以让他们都开心。

Everyone in the world calls there Flask instances app execpt for aws apperently.世界上的每个人都称那里为 Flask 实例应用程序执行 aws 显然。

I had to do two things to resolve the No module named 'application' error when deploying my flask app on elastic beanstalk:在弹性 beanstalk 上部署我的 flask 应用程序时,我必须做件事来解决No module named 'application'错误:

  1. As Quinten Cabo mentioned, I renamed the app variable:正如Quinten Cabo提到的,我重命名了app变量:
application = app = Flask(__name__)
  1. Additionally, I needed to rename the python file containing the app from app.py to application.py此外,我需要将包含应用程序的 python 文件从app.py重命名为application.py

暂无
暂无

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

相关问题 ModuleNotFoundError:在 AWS Elastic Beanstalk 上部署 Flask 应用程序时没有名为“应用程序”的模块 - ModuleNotFoundError: No module named 'application' when deploying Flask app on AWS Elastic Beanstalk ModuleNotFoundError:部署到 Elastic Beanstalk 时没有名为“django”的模块 - ModuleNotFoundError: No module named 'django' when deploying to Elastic Beanstalk 在 Elastic Beanstalk 上部署 Django 应用程序时出错 - Error Deploying Django App on Elastic Beanstalk Elastic Beanstalk Django 应用程序部署 502 错误网关服务器未运行(没有名为“应用程序”的模块) - Elastic Beanstalk Django app deployment 502 Bad Gateway Server not running (No module named: 'application') 无法在 AWS Elastic Beanstalk 上部署 Python Flask 应用程序 | 没有名为“应用程序”的模块 - Python Flask application not deployable on AWS Elastic Beanstalk | No module named 'application' 在 Elastic Beanstalk 上部署时找不到模块 Django - Cant find module Django while deploying on Elastic Beanstalk 部署到 Elastic Beanstalk 时出现 UnicodeDecodeError - UnicodeDecodeError while deploying to Elastic Beanstalk Elastic Beanstalk Flask 应用程序 - 没有名为“pandas_datareader”的模块 - Elastic Beanstalk Flask application - No module named 'pandas_datareader' 将 Django 应用程序部署到 Elastic Beanstalk 时出现内部服务器错误 500 - Internal Server Error 500 when deploying Django Application to Elastic Beanstalk 使用 CLI 将 flask 应用程序部署到弹性 beanstalk 时出现 502 错误 - 502 error deploying flask application to elastic beanstalk using CLI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM