Deploying Flask with Heroku Deploying Flask with Heroku flask flask

Deploying Flask with Heroku


In my Flask app hosted on Heroku, I use this code to start the server:

if __name__ == '__main__':    # Bind to PORT if defined, otherwise default to 5000.    port = int(os.environ.get('PORT', 5000))    app.run(host='0.0.0.0', port=port)

When developing locally, this will use port 5000, in production Heroku will set the PORT environment variable.

(Side note: By default, Flask is only accessible from your own computer, not from any other in the network (see the Quickstart). Setting host='0.0.0.0' will make Flask available from the network)


In addition to msiemens's answer

import osfrom run import app as applicationif __name__ == '__main__':    port = int(os.environ.get('PORT', 5000))    application.run(host='0.0.0.0', port=port)

Your Procfile should specify the port address which in this case is stored in the heroku environment variable ${PORT}

web: gunicorn --bind 0.0.0.0:${PORT} wsgi


Your main.py script cannot bind to a specific port, it needs to bind to the port number set in the $PORT environment variable. Heroku sets the port it wants in that variable prior to invoking your application.

The error you are getting suggests you are binding to a port that is not the one Heroku expects.