简体   繁体   English

Python Flask heroku应用程序错误

[英]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. 所以我确实有一个烧瓶应用程序,我可以在我的计算机本地运行它并成功部署到heroku但是当我heroku open ,网站继续说应用程序错误。

This is in my app.py: 这是在我的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: Procfile:

web: gunicorn app:app

Notes: 笔记:

I have everything setup including virtualenv and requirements.txt 我有一切设置,包括virtualenv和requirements.txt

I think a likely possibility is that that gunicorn is not using the correct port. 我认为很可能是那个枪炮没有使用正确的端口。 Heroku assigns a port for the application. Heroku为应用程序分配端口。 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: 但如果这是导致问题的原因,将Procfile更改为此应修复它:

web: gunicorn -b :$PORT app:app

This way catches whatever port assignment Heroku does. 这种方式捕获了Heroku所做的任何端口分配。 Or if you choose to set an environment variable for PORT it will also use that. 或者,如果您选择为PORT设置环境变量,它也将使用它。 gunicorn defaults to port 8000, so setting the PORT environment variable to 8000 on Heroku should also work. gunicorn默认为端口8000,因此在Heroku上将PORT环境变量设置为8000也应该可以工作。

I'm pretty sure that the app.run does not effect the gunicorn server in any way. 我很确定app.run不会以任何方式影响gunicorn服务器。 I think that gunicorn just finds the application instance in the module specified by app:app ( module:appinstance ), and loads the views. 我认为gunicorn只是在app:appmodule:appinstance )指定的模块中找到应用程序实例,并加载视图。

I created a file called Procfile in my root folder containing 我在我的根文件夹中创建了一个名为Procfile文件

web: gunicorn -b :$PORT app:app

Im my app.py , i have 我的app.py ,我有

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. Heroku dyno将设置它自己的PORT env,应用程序将使用它而不是我在本地使用的默认5000。

Working example at https://github.com/Sean-Bradley/Seans-Python3-Flask-Rest-Boilerplate https://github.com/Sean-Bradley/Seans-Python3-Flask-Rest-Boilerplate上的工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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