Question

I have a Flask application that works fine using the development webserver and uwsgi but when deployed to AWS Elastic Beanstalk all of my externally defined routes 404.

I'm using the larger application structure recommended here: http://flask.pocoo.org/docs/patterns/packages/

So I have an application.py that looks like this:

import settings
from flask import Flask

app = Flask(__name__)

app.config.from_object(settings)

application = app

#Ignore not used -> this pulls the views into the main app.
#see http://flask.pocoo.org/docs/patterns/packages/
import messages.views

#more imported applications here

@application.route("/", methods=['GET', 'POST'])
def hello():
    #health check endpoint.
    return "hi"

The hello method within application.py correctly returns 200, but the calls within message.views all return 404s.

Was it helpful?

Solution

As a test I decided to rename my application.py file to app.py and create a new application.py file with the following contents:

from app import app as application

This fixed the issue! I can now access all of my endpoints correctly.

Unfortunately I still don't understand what is wrong with the original setup.

OTHER TIPS

As mentioned in the http://flask.pocoo.org/docs/patterns/packages/ documentation in a flask app architecture like a package you get a directory structure that looks like :

    /yourapplication
        /yourapplication
            /__init__.py
            /static
                /style.css
            /templates
                layout.html
                index.html
                login.html
            /views
                /__init_.py
        ... 
    ...

And you put most of your code withing you __init__.py files so that you can call your views and application as modules of your package.

But as mentioned here you are still using application.py file which doesn't seems to be a part of package. Also While working in a architecture like this you need to be very careful about circular imports as mentioned on the same link near the end of the webpage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top