[英]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'
错误:
app
variable:正如Quinten Cabo提到的,我重命名了app
变量:application = app = Flask(__name__)
app.py
to application.py
此外,我需要将包含应用程序的 python 文件从app.py
重命名为application.py
2.In container options change the WSGIPath from application to your_project_name.wsgi:application 2.在容器选项中,将 WSGIPath 从application更改为your_project_name.wsgi:application
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.