Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Flask with Heroku

Tags:

heroku

flask

I'm trying to deploy a Flask app to Heroku however upon pushing the code I get the error

2013-06-23T11:23:59.264600+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch  

I'm not sure what to try, I've tried changing the port from 5000 to 33507, but to no avail. My Procfile looks like this:

web: python main.py  

main.py is the main Flask file which initiates the server.

Thanks.

like image 939
Shannon Rothe Avatar asked Jun 23 '13 11:06

Shannon Rothe


People also ask

How do you deploy a python application on Heroku?

Install app dependencies locally txt in the root directory is one way for Heroku to recognize your Python app. The requirements. txt file lists the app dependencies together. When an app is deployed, Heroku reads this file and installs the appropriate Python dependencies using the pip install -r command.

Where can I deploy Flask app for free?

Flask deployment To deploy your Flask app, you can use PythonAnywhere. This puts your app online, for anyone to access. They maintain the server for you, so you don't have to. On top of that, it's free for small apps.

How do I deploy a Flask app to Heroku from GitHub?

On Heroku, when it comes to deploying web apps written with Flask, it requires you to download the Heroku CLI and then proceed further. Well, there's another way to do it as well and that is by installing dependencies on the go and deploying the Flask app by connecting the GitHub repository to your Heroku app.


2 Answers

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)

like image 111
msiemens Avatar answered Oct 04 '22 09:10

msiemens


In addition to msiemens's answer

import os from run import app as application if __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

like image 35
Mayur Rawte Avatar answered Oct 04 '22 11:10

Mayur Rawte