首页 > 解决方案 > Heroku Flask 应用程序成功提交但收到“应用程序错误”

问题描述

我有一个 Flask 应用程序,它使用 OpenCV 从照片中检测人脸。我正在使用 Heroku 和 GitHub 来使这个网站上线,但我一直收到“应用程序错误”

我发现的最有用的教程是https://medium.com/analytics-vidhya/deploying-your-opencv-flask-web-application-on-heroku-c23efcceb1e8。其中我完全按照他的做法进行操作,但仍然收到“应用程序错误”。但是当我使用他的文件夹和脚本时,他的网站就会上线。但是当我三次检查我的文档中的所有内容是否与他的匹配时,我的 Flask 应用程序仍然收到“应用程序错误”

目录树 https://i.imgur.com/l9euimF.png

不知道发生了什么,这是我最近的构建日志

从 Heroku 构建日志

-----> Building on the Heroku-20 stack
-----> Apt app detected
-----> Reusing cache
-----> Updating apt caches
       Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
       Hit:2 http://apt.postgresql.org/pub/repos/apt focal-pgdg InRelease
       Get:3 http://archive.ubuntu.com/ubuntu focal-security InRelease [109 kB]
       Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
       Fetched 223 kB in 1s (317 kB/s)
       Reading package lists...
-----> Fetching .debs for libsm6
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/16.1 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libxrender1
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/18.7 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libfontconfig1
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/114 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libice6
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/41.0 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Installing libfontconfig1_2.13.1-2ubuntu3_amd64.deb
-----> Installing libice6_2%3a1.0.10-0ubuntu1_amd64.deb
-----> Installing libsm6_2%3a1.2.3-1_amd64.deb
-----> Installing libxrender1_1%3a0.9.10-1_amd64.deb
-----> Writing profile script
-----> Rewrite package-config files
-----> Apt app detected
-----> Reusing cache
-----> Updating apt caches
       Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
       Hit:2 http://apt.postgresql.org/pub/repos/apt focal-pgdg InRelease
       Get:3 http://archive.ubuntu.com/ubuntu focal-security InRelease [109 kB]
       Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
       Fetched 223 kB in 1s (349 kB/s)
       Reading package lists...
-----> Fetching .debs for libsm6
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/16.1 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libxrender1
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/18.7 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libfontconfig1
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/114 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libice6
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/41.0 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Installing libfontconfig1_2.13.1-2ubuntu3_amd64.deb
-----> Installing libice6_2%3a1.0.10-0ubuntu1_amd64.deb
-----> Installing libsm6_2%3a1.2.3-1_amd64.deb
-----> Installing libxrender1_1%3a0.9.10-1_amd64.deb
-----> Writing profile script
-----> Rewrite package-config files
-----> Python app detected
 !     Python has released a security update! Please consider upgrading to python-3.8.8
       Learn More: https://devcenter.heroku.com/articles/python-runtimes
-----> No change in requirements detected, installing from cache
-----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
-----> Installing SQLite3
-----> Installing requirements with pip
-----> Apt app detected
-----> Reusing cache
-----> Updating apt caches
       Hit:1 http://apt.postgresql.org/pub/repos/apt focal-pgdg InRelease
       Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
       Get:3 http://archive.ubuntu.com/ubuntu focal-security InRelease [109 kB]
       Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
       Fetched 223 kB in 1s (330 kB/s)
       Reading package lists...
-----> Fetching .debs for libsm6
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/16.1 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libxrender1
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/18.7 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libfontconfig1
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/114 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Fetching .debs for libice6
       Reading package lists...
       Building dependency tree...
       0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 20 not upgraded.
       Need to get 0 B/41.0 kB of archives.
       After this operation, 0 B of additional disk space will be used.
       Download complete and in download only mode
-----> Installing libfontconfig1_2.13.1-2ubuntu3_amd64.deb
-----> Installing libice6_2%3a1.0.10-0ubuntu1_amd64.deb
-----> Installing libsm6_2%3a1.2.3-1_amd64.deb
-----> Installing libxrender1_1%3a0.9.10-1_amd64.deb
-----> Writing profile script
-----> Rewrite package-config files
-----> Discovering process types
       Procfile declares types -> web
-----> Compressing...
       Done: 188.3M
-----> Launching...
       Released v21
       https://lazereyes.herokuapp.com/ deployed to Heroku

应用程序.py

import imghdr
import os
import numpy as np
import cv2
from PIL import Image
from os.path import join, dirname, realpath
from flask import Flask, render_template, request, redirect, url_for, abort
from werkzeug.utils import secure_filename
import json

# Code...

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host="0.0.0.0", port=port)

要求.txt

click==7.1.2
Flask==1.1.2
gunicorn==20.0.4
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
numpy==1.20.1
opencv-python==4.5.1.48
Pillow==8.1.2
Werkzeug==1.0.1

档案

web: gunicorn --bind 0.0.0.0:$PORT app:app

运行时.txt

python-3.8.5

文件

libsm6
libxrender1
libfontconfig1
libice6

标签: pythonopencvflaskheroku

解决方案


在没有看到确切的错误消息的情况下,我可以想到两个问题:

  1. 您应该检查 Heroku 中是否设置了FLASK_ENV,FLASK_APPPORT环境变量。

  2. 您应该检查在 Heroku 上运行的图像是否使用与您的代码兼容的 Python 版本。就我而言,这是解决与您类似的问题的方法。


推荐阅读