Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Flask app to Heroku

Tags:

I'm trying to develop my first "large" app with Flask on Heroku and I'm attempting to combine the basic tutorial here: https://devcenter.heroku.com/articles/python with the instructions here: http://flask.pocoo.org/docs/patterns/packages/#larger-applications. It works locally with "foreman start" but when I push to Heroku I get an error that the wrong port is in use:

Starting process with command python run.py 2012-12-04T23:45:18+00:00 app[web.1]: * Running on http://127.0.0.1:5000/ 2012-12-04T23:45:18+00:00 app[web.1]: * Restarting with reloader 2012-12-04T23:45:23+00:00 heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 5000, should be 33507 (see environment variable PORT)

I'm new to all this, but it looks like it's trying to run "locally" on Heroku. I've tried all sorts of combinations, but can't get it to work. My very simple code right now is (the app is called "pml"):

directory: /pml

Procfile:

web: python run.py 

run.py:

from pml import app app.run(debug=True) 

directory: /pml/pml/

__init__.py

from flask import Flask app = Flask(__name__)  import pml.views 

view.py

from pml import app  @app.route('/') def index():     return 'Hello World!' 
like image 227
crix Avatar asked Dec 05 '12 00:12

crix


People also ask

How do I deploy flask app with Heroku?

Deploying Flask App on HerokuSTEP 1 : Create a virtual environment with pipenv and install Flask and Gunicorn . STEP 2 : Create a “Procfile” and write the following code. STEP 3 : Create “runtime. txt” and write the following code.

How do I deploy a Python application in 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.


1 Answers

I haven't used Heroku, but to me, it looks like they have a reserved port for Flask, specifically 33507. It looks like it will try to use an environment variable, which I am not sure how to set in Heroku. The good news is you can tell Flask which port to use.

try this:

app.run(debug=True, port=33507) 

and it looks like adding the PORT to the env in heroku is done like this:

heroku config:add PORT=33507 

You should only have to do one of these. I would try the first as it, to me, is the straight forward way to fix the issue.

EDIT
After reading the article from your post, I see where the issue comes in.

port = int(os.environ.get('PORT', 5000)) 

That line says, get the value of PORT from the environment if it is set, otherwise use 5000. I am not sure why they wouldn't allow it to run from 5000 if that's what is in their docs, but I would try this change:

port = int(os.environ.get('PORT', 33507)) 
like image 84
sberry Avatar answered Oct 11 '22 10:10

sberry