无法在“main”中找到应用程序对象“app” - Heroku CLI

Wid*_*les 2 heroku flask

我尝试将此 GitHub 存储库中的代码推送到我的 Heroku 应用程序,但我不断收到相同的错误,并且我的进程不断以错误 1 ​​退出。我查看了这些帖子来解决我的问题:

gunicorn 导致 heroku Flask 应用程序 错误无法在 heroku 服务器上启动Python Flask heroku 应用程序错误

主文件

import os
import sys
import urllib.request
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from flask import Flask, render_template, request, redirect

ic = Flask(__name__)

count = 0


@ic.route("/")
def main():
    if count == 1:
        return render_template("index.html", result=str((str(count) + " Image Downloaded !")))
    else:
        return render_template("index.html", result=str((str(count) + " Images Downloaded !")))


@ic.route("/get_images", methods=['POST'])
def get_images():
    _url = request.form['inputURL']
    try:
        global count
        count = 0
        code = requests.get(_url)
        text = code.text
        soup = BeautifulSoup(text)
        for img in soup.findAll('img'):
            count += 1
            if (img.get('src'))[0:4] == 'http':
                src = img.get('src')
            else:
                src = urljoin(_url, img.get('src'))
            download_image(src, count)
        return redirect("http://localhost:5000")
    except requests.exceptions.HTTPError as error:
        return render_template("index.html", result=str(error))


def download_image(url, num):
    try:
        image_name = str(num) + ".jpg"
        image_path = os.path.join("images/", image_name)
        urllib.request.urlretrieve(url, image_path)
    except ValueError:
        print("Invalid URL !")
    except:
        print("Unknown Exception" + str(sys.exc_info()[0]))


if __name__ == "__main__":
    ic.run()
Run Code Online (Sandbox Code Playgroud)

配置文件

heroku ps:scale web=1
web: gunicorn main:app
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 5

当你跑

gunicorn main:app
Run Code Online (Sandbox Code Playgroud)

告诉gunicorn你应用程序的入口点是模块中的app变量main。你没有app变量;您的 Flask 应用程序名为ic.

你也不需要那条heroku ps:scale web=1线。这是您可以在本地机器上运行的命令,以将您的web进程类型扩展到一个 dyno。

改变你Procfile

web: gunicorn main:ic
Run Code Online (Sandbox Code Playgroud)

提交和重新部署。