Heroku应用程序错误-尽管部署成功

问题描述

背景
我有一个Flask应用,该应用带有RESTful API和我要部署到Heroku的网页。作为一个基于Python的(app.py)项目,我显然部署了“ python方法”。要求包括一个requirements.txt文件

问题:
我做了一个pip freeze,并在我的终端机中充满了许多图书馆。我想出了什么,我将全部内容复制到requirements.txt(是的,我拼写正确!),然后将其保存在项目的根目录下,并与main.py文件一起。

在部署Heroku时,它会安装要求等,并说应用已成功部署,但是当我导航到我的项目时,会得到一个Heroku通用的“应用程序”错误消息(here)。

有用的信息:

日志:是我最近的构建尝试-

-----> Python app detected
-----> Requirements file has been changed,clearing cached dependencies
-----> Installing python-3.6.12
-----> Installing pip 20.1.1,setuptools 47.1.1 and wheel 0.34.2
-----> Installing sqlite3
-----> Installing requirements with pip
       Collecting Flask==1.1.2
         Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
       Collecting Flask-Bootstrap==3.3.7.1
         Downloading Flask-Bootstrap-3.3.7.1.tar.gz (456 kB)
       Collecting flask-marshmallow==0.13.0
         Downloading flask_marshmallow-0.13.0-py2.py3-none-any.whl (9.9 kB)
       Collecting Flask-PyMongo==2.3.0
         Downloading Flask_PyMongo-2.3.0-py2.py3-none-any.whl (12 kB)
       Collecting Flask-RESTful==0.3.8
         Downloading Flask_RESTful-0.3.8-py2.py3-none-any.whl (25 kB)
       Collecting Flask-sqlAlchemy==2.4.4
         Downloading Flask_sqlAlchemy-2.4.4-py2.py3-none-any.whl (17 kB)
       Collecting flaskerizer==0.0.8
         Downloading flaskerizer-0.0.8.tar.gz (17 kB)
       Collecting pymongo==3.11.0
         Downloading pymongo-3.11.0-cp36-cp36m-manylinux2014_x86_64.whl (508 kB)
       Collecting json2html==1.3.0
         Downloading json2html-1.3.0.tar.gz (7.0 kB)
       Collecting json5==0.9.5
         Downloading json5-0.9.5-py2.py3-none-any.whl (17 kB)
       Collecting beautifulsoup4==4.9.1
         Downloading beautifulsoup4-4.9.1-py3-none-any.whl (115 kB)
       Collecting Jinja2>=2.10.1
         Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB)
       Collecting itsdangerous>=0.24
         Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
       Collecting click>=5.1
         Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
       Collecting Werkzeug>=0.15
         Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
       Collecting dominate
         Downloading dominate-2.5.1-py2.py3-none-any.whl (29 kB)
       Collecting visitor
         Downloading visitor-0.1.3.tar.gz (3.3 kB)
       Collecting marshmallow>=2.0.0
         Downloading marshmallow-3.7.1-py2.py3-none-any.whl (45 kB)
       Collecting six>=1.9.0
         Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
       Collecting aniso8601>=0.82
         Downloading aniso8601-8.0.0-py2.py3-none-any.whl (43 kB)
       Collecting pytz
         Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
       Collecting sqlAlchemy>=0.8.0
         Downloading sqlAlchemy-1.3.19-cp36-cp36m-manylinux2010_x86_64.whl (1.3 MB)
       Collecting numpy
         Downloading numpy-1.19.1-cp36-cp36m-manylinux2010_x86_64.whl (14.5 MB)
       Collecting soupsieve>1.2
         Downloading soupsieve-2.0.1-py3-none-any.whl (32 kB)
       Collecting MarkupSafe>=0.23
         Downloading MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl (27 kB)
       Building wheels for collected packages: Flask-Bootstrap,flaskerizer,json2html,visitor
         Building wheel for Flask-Bootstrap (setup.py): started
         Building wheel for Flask-Bootstrap (setup.py): finished with status 'done'
         Created wheel for Flask-Bootstrap: filename=Flask_Bootstrap-3.3.7.1-py3-none-any.whl size=460123 sha256=9bd9e63bb6b77d2a2750200e52bb64f47e327c55fcf5d53e56273422c6897236
         Stored in directory: /tmp/pip-ephem-wheel-cache-n6hu4_1p/wheels/5b/f4/4f/26886e9f06b0c1b6d54104a1fb423ebc7e06d94cf69700348e
         Building wheel for flaskerizer (setup.py): started
         Building wheel for flaskerizer (setup.py): finished with status 'done'
         Created wheel for flaskerizer: filename=flaskerizer-0.0.8-py3-none-any.whl size=19420 sha256=651ddb678fae4fa5961d8a95cab2569b2b8a3adefed4440b80fb3bba26a95aa7
         Stored in directory: /tmp/pip-ephem-wheel-cache-n6hu4_1p/wheels/cc/1d/54/3c80ed968fd156ae6a6b44923127522538f8ade767dbd8d256
         Building wheel for json2html (setup.py): started
         Building wheel for json2html (setup.py): finished with status 'done'
         Created wheel for json2html: filename=json2html-1.3.0-py3-none-any.whl size=7596 sha256=cab0c712dd167dda857c2fa995b731fc5be5644adcd3a4425a8d8a33da05749e
         Stored in directory: /tmp/pip-ephem-wheel-cache-n6hu4_1p/wheels/ac/31/34/ddfd5ecba08389f839470d849288be216fcb22f1403eaa05df
         Building wheel for visitor (setup.py): started
         Building wheel for visitor (setup.py): finished with status 'done'
         Created wheel for visitor: filename=visitor-0.1.3-py3-none-any.whl size=3931 sha256=4ae43104c4bd7035ce0d468ea846a663c66444810b67338ef9df91be7245bcf8
         Stored in directory: /tmp/pip-ephem-wheel-cache-n6hu4_1p/wheels/a9/77/93/3b8a22ac46f57a22d0fd7b4912fff740bcbddbd720bd48508e
       Successfully built Flask-Bootstrap flaskerizer json2html visitor
       Installing collected packages: MarkupSafe,Jinja2,itsdangerous,click,Werkzeug,Flask,dominate,visitor,Flask-Bootstrap,marshmallow,six,flask-marshmallow,pymongo,Flask-PyMongo,aniso8601,pytz,Flask-RESTful,sqlAlchemy,Flask-sqlAlchemy,numpy,json5,soupsieve,beautifulsoup4
       Successfully installed Flask-1.1.2 Flask-Bootstrap-3.3.7.1 Flask-PyMongo-2.3.0 Flask-RESTful-0.3.8 Flask-sqlAlchemy-2.4.4 Jinja2-2.11.2 MarkupSafe-1.1.1 sqlAlchemy-1.3.19 Werkzeug-1.0.1 aniso8601-8.0.0 beautifulsoup4-4.9.1 click-7.1.2 dominate-2.5.1 flask-marshmallow-0.13.0 flaskerizer-0.0.8 itsdangerous-1.1.0 json2html-1.3.0 json5-0.9.5 marshmallow-3.7.1 numpy-1.19.1 pymongo-3.11.0 pytz-2020.1 six-1.15.0 soupsieve-2.0.1 visitor-0.1.3
-----> discovering process types
       procfile declares types -> (none)
-----> Compressing...
       Done: 75.5M
-----> Launching...
       Released v7
       https://project-2-website.herokuapp.com/ deployed to Heroku

烧瓶代码

import flask
from flask import Flask,jsonify,Response,render_template
from flask import redirect
import pymongo
from pymongo import MongoClient
from bson import ObjectId,json_util
import json

cluster = pymongo.MongoClient("mongodb+srv://USERNAME:PASSWORD@cluster0.mpjcg.mongodb.net/<dbname>?retryWrites=true&w=majority")
db = cluster["simply_recipe"]
collection = db["recipes_collection"]

app = Flask(__name__)

# This route returns the team's index page
@app.route("/")
def home():
    return render_template('index.html')

# This route returns heesung's plot page of the team's website
@app.route("/heesung/")
def heesung():
    return redirect("https://heesung80.github.io/recipe/")

# This route returns caitlin's plot page of the team's website
@app.route("/caitlin")
def caitlin_plots():
    return render_template('inner-page_caitlin.html')

# This route returns all the recipe_collection data in JSON.
@app.route("/recipes",methods=["GET"])
def get_recipes():
    all_recipes = list(collection.find({}))
    return json.dumps(all_recipes,default=json_util.default)

if __name__ == "__main__":
    app.run()

有人对我的Flask应用程序为何成功部署但出现应用程序错误有任何建议吗?

解决方法

您已经包含了构建日志,但是它在您的应用程序日志中说了什么? logs命令默认检索100条日志行,因此在浏览器中访问您的应用,然后在您的终端机heroku logs -a name-of-your-app(或view logs from the UI)中运行它。希望这会给您带来更好的查找错误。为了更轻松地查看日志,您应该在应用程序中添加日志记录插件(Papertrail has a free plan,可以使用)。

如果不查看日志,我想这与建立与MongoDB的连接是一个问题。我无法判断USERNAME的{​​{1}},PASSWORD<dbname>的值来自哪里,但是由于您使用的是"mongodb+srv://USERNAME:PASSWORD@cluster0.mpjcg.mongodb.net/<dbname>?retryWrites=true&w=majority",所以我假设您是使用MongoDB Atlas。我自己没有经验,但是文档表明您需要whitelist IPs建立连接。如果您的应用程序在“专用空间”中,则可以将your space's outbound IPs与Mongo一起列入白名单,但是如果该应用程序在“通用运行时”中,则您需要将+srv列入白名单,因为“通用运行时”没有静态出站IP。 / p>

您的连接字符串也应在应用程序上设置为config var

,

从评论和在线研究中获得建议后,我注意到,当您部署到Heroku时,Heroku尝试使用Pipfile而非Procfile来构建我的环境。

解决方案:
,通过删除Pipfile,Heroku退回到使用Procfile来确保构建和部署成功。