简体   繁体   中英

Python Flask heroku application error

So I do have a flask app, I can run it in my computer locally and deployed it to heroku successfully but when I do heroku open the site keeps on saying Application Error.

This is in my app.py:

import random
import os

from flask import Flask
from gen_histogram import histogram
from sample import generate_probability, generate_word

app = Flask(__name__)

dict_histogram = histogram('tom_sawyer.txt')

tes_list = ["one", "fish", "two", "fish", "red", "fish", "blue", "fish"]


def try_random():
   return random.choice(tes_list)


@app.route('/')
def hello_world():
   return try_random()


if __name__ == '__main__':
   port = int(os.environ.get("PORT", 5000))
   app.run(debug=True, port=port)

Procfile:

web: gunicorn app:app

Notes:

I have everything setup including virtualenv and requirements.txt

I think a likely possibility is that that gunicorn is not using the correct port. Heroku assigns a port for the application. I'm not entirely sure if that port gets assigned randomly or if it has a default. But if this is what is causing the problem, changing the Procfile to this should fix it:

web: gunicorn -b :$PORT app:app

This way catches whatever port assignment Heroku does. Or if you choose to set an environment variable for PORT it will also use that. gunicorn defaults to port 8000, so setting the PORT environment variable to 8000 on Heroku should also work.

I'm pretty sure that the app.run does not effect the gunicorn server in any way. I think that gunicorn just finds the application instance in the module specified by app:app ( module:appinstance ), and loads the views.

I created a file called Procfile in my root folder containing

web: gunicorn -b :$PORT app:app

Im my app.py , i have

import os
...
port = int(os.environ.get('PORT', 5000))
...
app.run(host='0.0.0.0', port=port, debug=True)

Heroku dyno will set it's own PORT env and the app will use that rather than the default 5000 i use locally.

Working example at https://github.com/Sean-Bradley/Seans-Python3-Flask-Rest-Boilerplate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM