-1

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

问题:
我做了一个pip freeze并且有一吨图书馆填满了我的终端。我想'到底是什么,我会将整个内容复制到一个requirements.txt(是的,我拼写正确!)并保存在我的项目的根目录中,与我的main.py文件一起。

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

有用信息:

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

-----> 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, flaskerizer, json2html, 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 应用程序部署成功,但给出了应用程序错误?

4

2 回答 2

0

您已经包含了构建日志,但它在您的应用程序日志中说明了什么?该logs命令默认检索 100 条日志行,因此请在浏览器中访问您的应用程序,然后在您的终端中运行它:(heroku logs -a name-of-your-app从 UI 中查看日志)。这应该会给你一个更好的错误来查找。为了更轻松地查看日志,您应该在应用程序中添加一个日志插件(Papertrail 有一个免费计划可供您使用)。

如果不查看您的日志,我猜这是与 MongoDB 建立连接的问题。我不知道 和 的值来自哪里,但USERNAME由于您使用的是,我假设您使用的是 MongoDB Atlas。我自己没有这方面的经验,但文档表明您需要将IP 列入白名单才能建立连接。如果您的应用在 Private Spaces 中,您可以使用 Mongo将您空间的出站 IP列入白名单,但如果应用在 Common Runtime 中,则您需要将其列入白名单,因为 Common Runtime 没有静态出站 IP。PASSWORD<dbname>"mongodb+srv://USERNAME:PASSWORD@cluster0.mpjcg.mongodb.net/<dbname>?retryWrites=true&w=majority"+srv0.0.0.0/0

您的连接字符串也应该在应用程序上设置为配置变量

于 2020-08-18T16:17:07.720 回答
0

从评论和在线研究中获得建议后,我注意到当您部署到 Heroku 时,Heroku 尝试使用Pipfile. 而不是Procfile.

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

于 2020-08-19T19:47:05.207 回答