将 Flask 应用程序部署到 Heroku [英] Deploying Flask app to Heroku

查看:36
本文介绍了将 Flask 应用程序部署到 Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Heroku 上使用 Flask 开发我的第一个大型"应用程序,并且我正在尝试结合此处的基本教程:https://devcenter.heroku.com/articles/python 和这里的说明:http://flask.pocoo.org/docs/patterns/packages/#larger-applications.它在本地与工头开始"一起工作,但是当我推送到 Heroku 时,我收到一个错误,指出正在使用错误的端口:

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:

使用命令python run.py启动进程2012-12-04T23:45:18+00:00 app[web.1]: * 运行http://127.0.0.1:5000/ 2012-12-04T23:45:18+00:00 应用[web.1]:*用重新加载器重新启动 2012-12-04T23:45:23+00:00 heroku[web.1]:错误 R11(错误绑定)-> 进程绑定到端口 5000,应该是 33507(见环境变量PORT)

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)

我对这一切都不熟悉,但看起来它正试图在 Heroku 上本地"运行.我尝试了各种组合,但无法让它发挥作用.我现在非常简单的代码是(该应用程序称为pml"):

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"):

目录:/pml

简介:

web: python run.py

run.py:

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

目录:/pml/pml/

directory: /pml/pml/

__init__.py

__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!'

推荐答案

我没用过 Heroku,但对我来说,他们似乎为 Flask 预留了一个端口,特别是 33507.看起来它会尝试使用一个环境变量,我不确定如何在 Heroku 中设置.好消息是你可以告诉 Flask 使用哪个端口.

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.

试试这个:

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

看起来像将 PORT 添加到 heroku 中的 env 是这样完成的:

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))

那行说,如果设置了 PORT 从环境中获取值,否则使用 5000.我不知道为什么他们不允许它从 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))

这篇关于将 Flask 应用程序部署到 Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆