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

Question:

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

I get a 502 error when going to the site, and degraded/severe health on the environment. 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:


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:

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. 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. And I should note that the web app works when I just run the python code.

Asked By: Hunter Swag

||

Answers:

A possible reason is the use of Amazon Linux 2 environment, instead of Amazon Linux 1.

The list of python environments and their linux distributions is here.

From the link you provided:

In this tutorial we use Python 3.6 and the corresponding Elastic Beanstalk platform version.

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.

Answered By: Marcin

I had the same issue as you. Downgrading to AL1 does solve this but brings more issues.

I found that you can make your app compatible by adding the following line to the end of the file.

app = application

Alternatively you can do this when you define the flask instance:

application = app = Flask(__name__)
do not forget to import both into application.py if needed.

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.

Everyone in the world calls there Flask instances app except for AWS apparently.

Answered By: QuintenC

I had to do two things to resolve the No module named 'application' error when deploying my flask app on elastic beanstalk:

  1. As Quinten Cabo mentioned, I renamed the app variable:
application = app = Flask(__name__)
  1. Additionally, I needed to rename the python file containing the app from app.py to application.py
Answered By: BenPortner
  1. Go to configuration and then software

  2. In container options change the WSGIPath from "application" to "your_project_name.wsgi:application"

Container Options

Answered By: Last Saint