将Flask应用程序部署到Heroku时,出现“ ModuleNotFoundError:没有名为“ app”的模块”

时间:2020-06-11 22:19:57

标签: python python-3.x flask heroku

在尝试将Flask应用程序部署到Heroku时出现错误。我在下面提供一些文件。它一直显示屏幕提示应用程序错误。我已经在系统上本地测试了我的代码,它可以完美运行,并且在部署任何一个时都没有遇到任何问题。只是当我打开网站时,它给我一个错误。

main.py

from flask import Flask, render_template, send_file

app = Flask(__name__)

@app.route('/Games')
def games():
    return render_template('games.html')


@app.route("/Play_BombMania")
def bombmania():
    return render_template('download_bombMania.html')

@app.route("/BombMania")
def dbm():
    try:
        return send_file('C:/MY DATA/python projects/my website/static/Bomb-Mania.zip', attachment_filename='Bomb-Mania.zip')
    except Exception as e:
        return str(e)

@app.route('/')
@app.route('/home')
def home():
    return render_template('home.html')

@app.route('/Play_Shoot-a-Mole')
def shootamole():
    return render_template('download_shootaMole.html')

@app.route('/Shoot-a-Mole')
def dsam():
    try:
        return send_file('C:/MY DATA/python projects/my website/static/Shoot-a-Mole.zip', attachment_filename='Shoot-a-Mole.zip')
    except Exception as e:
        return str(e)


if __name__ ==  '__main__':
    app.run(debug = False)

Procfile

web: gunicorn -b :$PORT app:app

base.html

   <!DOCTYPE html>
<html>
<head>
  <div>
  {% block style %}
  {% endblock %}
</div>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <title>RBGamez</title>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light", style=" background-color: #3293a8">
  <a class="navbar-brand" href="#">RBGamez</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="{{url_for('home')}}">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="{{url_for('games')}}">My Games</a>
      </li>
<!--       <li class="nav-item dropdown"> -->
<!--         <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a> -->
<!--         <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form> -->
  </div>
</nav>
</nav>
<div class = "content">
  {% block content %}
  {% endblock %}

</div>
<div>
{% block body %}
{% endblock %}
</div>
</body>
</html>

Requirements.txt

certifi==2020.4.5.1
click==7.1.2
Flask==1.1.2
gunicorn==20.0.4
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1
wincertstore==0.2

我的日志

2020-06-12T04:38:01.837780+00:00 heroku[web.1]: Starting process with command `gunicorn -b :10848 app:app`
2020-06-12T04:38:04.000000+00:00 app[api]: Build succeeded
2020-06-12T04:38:04.828048+00:00 app[web.1]: [2020-06-12 04:38:04 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-06-12T04:38:04.829051+00:00 app[web.1]: [2020-06-12 04:38:04 +0000] [4] [INFO] Listening at: http://0.0.0.0:10848 (4)
2020-06-12T04:38:04.829239+00:00 app[web.1]: [2020-06-12 04:38:04 +0000] [4] [INFO] Using worker: sync
2020-06-12T04:38:04.834432+00:00 app[web.1]: [2020-06-12 04:38:04 +0000] [10] [INFO] Booting worker with pid: 10
2020-06-12T04:38:04.840073+00:00 app[web.1]: [2020-06-12 04:38:04 +0000] [10] [ERROR] Exception in worker process
2020-06-12T04:38:04.840074+00:00 app[web.1]: Traceback (most recent call last):
2020-06-12T04:38:04.840075+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-06-12T04:38:04.840075+00:00 app[web.1]: worker.init_process()
2020-06-12T04:38:04.840075+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-06-12T04:38:04.840076+00:00 app[web.1]: self.load_wsgi()
2020-06-12T04:38:04.840076+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-06-12T04:38:04.840076+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-06-12T04:38:04.840076+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-06-12T04:38:04.840077+00:00 app[web.1]: self.callable = self.load()
2020-06-12T04:38:04.840077+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-06-12T04:38:04.840077+00:00 app[web.1]: return self.load_wsgiapp()
2020-06-12T04:38:04.840078+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-06-12T04:38:04.840078+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-06-12T04:38:04.840078+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-06-12T04:38:04.840078+00:00 app[web.1]: mod = importlib.import_module(module)
2020-06-12T04:38:04.840078+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-06-12T04:38:04.840079+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-06-12T04:38:04.840079+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-06-12T04:38:04.840079+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-06-12T04:38:04.840079+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
2020-06-12T04:38:04.840080+00:00 app[web.1]: ModuleNotFoundError: No module named 'app'
2020-06-12T04:38:04.840191+00:00 app[web.1]: [2020-06-12 04:38:04 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-06-12T04:38:04.868489+00:00 app[web.1]: [2020-06-12 04:38:04 +0000] [4] [INFO] Shutting down: Master
2020-06-12T04:38:04.868590+00:00 app[web.1]: [2020-06-12 04:38:04 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-06-12T04:38:04.946583+00:00 heroku[web.1]: Process exited with status 3
2020-06-12T04:38:04.990388+00:00 heroku[web.1]: State changed from starting to crashed
2020-06-12T04:38:04.992705+00:00 heroku[web.1]: State changed from crashed to starting
2020-06-12T04:38:10.595018+00:00 heroku[web.1]: Starting process with command `gunicorn -b :55195 app:app`
2020-06-12T04:38:13.581474+00:00 app[web.1]: [2020-06-12 04:38:13 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-06-12T04:38:13.582087+00:00 app[web.1]: [2020-06-12 04:38:13 +0000] [4] [INFO] Listening at: http://0.0.0.0:55195 (4)
2020-06-12T04:38:13.582188+00:00 app[web.1]: [2020-06-12 04:38:13 +0000] [4] [INFO] Using worker: sync
2020-06-12T04:38:13.587025+00:00 app[web.1]: [2020-06-12 04:38:13 +0000] [10] [INFO] Booting worker with pid: 10
2020-06-12T04:38:13.592047+00:00 app[web.1]: [2020-06-12 04:38:13 +0000] [10] [ERROR] Exception in worker process
2020-06-12T04:38:13.592048+00:00 app[web.1]: Traceback (most recent call last):
2020-06-12T04:38:13.592049+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-06-12T04:38:13.592049+00:00 app[web.1]: worker.init_process()
2020-06-12T04:38:13.592049+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-06-12T04:38:13.592049+00:00 app[web.1]: self.load_wsgi()
2020-06-12T04:38:13.592050+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-06-12T04:38:13.592050+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-06-12T04:38:13.592050+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-06-12T04:38:13.592051+00:00 app[web.1]: self.callable = self.load()
2020-06-12T04:38:13.592051+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-06-12T04:38:13.592051+00:00 app[web.1]: return self.load_wsgiapp()
2020-06-12T04:38:13.592052+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-06-12T04:38:13.592052+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-06-12T04:38:13.592052+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-06-12T04:38:13.592052+00:00 app[web.1]: mod = importlib.import_module(module)
2020-06-12T04:38:13.592068+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-06-12T04:38:13.592068+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-06-12T04:38:13.592069+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-06-12T04:38:13.592069+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-06-12T04:38:13.592069+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
2020-06-12T04:38:13.592074+00:00 app[web.1]: ModuleNotFoundError: No module named 'app'
2020-06-12T04:38:13.592167+00:00 app[web.1]: [2020-06-12 04:38:13 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-06-12T04:38:13.622299+00:00 app[web.1]: [2020-06-12 04:38:13 +0000] [4] [INFO] Shutting down: Master
2020-06-12T04:38:13.622439+00:00 app[web.1]: [2020-06-12 04:38:13 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-06-12T04:38:13.710801+00:00 heroku[web.1]: Process exited with status 3
2020-06-12T04:38:13.761225+00:00 heroku[web.1]: State changed from starting to crashed
2020-06-12T04:38:16.833579+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=rbgamez.herokuapp.com request_id=8427e0a6-eb46-4dd4-8c29-248350c01d8d fwd="182.69.74.152" dyno= connect= service= status=503 bytes= protocol=https
2020-06-12T04:38:17.489625+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=rbgamez.herokuapp.com request_id=bb27d81d-d4d2-43a3-b884-fdf7a90dcd30 fwd="182.69.74.152" dyno= connect= service= status=503 bytes= protocol=https

1 个答案:

答案 0 :(得分:1)

在调试Heroku部署错误时,最好检查一下Heroku日志:

heroku[web.1]: Starting process with command `gunicorn -b :10848 app:app`
...
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
app[web.1]: worker.init_process()
app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
app[web.1]: self.load_wsgi()
...
app[web.1]: ModuleNotFoundError: No module named 'app'

似乎Procfile中的gunicorn命令不正确。

web: gunicorn -b :$PORT app:app

gunicorn docs中,应该是:

$ gunicorn [OPTIONS] APP_MODULE

APP_MODULE的模式为$(MODULE_NAME):$(VARIABLE_NAME)。 模块名称可以是完整的点分路径。变量名称是指 可以在指定模块中找到的WSGI可调用项。

由于您在app = Flask(__name__)中定义了Flask应用实例(main.py),因此模块名称为main(而不是app),因此Procfile应该包含:

web: gunicorn -b :$PORT main:app

这就是为什么Heroku告诉您找不到该模块的原因:

app[web.1]: ModuleNotFoundError: No module named 'app'