Heroku/Flask deployment failing, either 'can't find attribute app' or 'can't find module name (flask_module)', proc file to blame?

一曲冷凌霜 提交于 2020-02-07 02:01:31

问题


I'm trying to deploy a Flask application to heroku and I'm suspicious that perhaps the Procfile is to blame. I'm not sure if I wrote it correctly, and liike many other stackoverflow users I had a hard time finding documentation to clarify what the syntax of the Procfile is supposed to be. I'm using gunicorn just cuz tutorials seemed to recommend it.

First off, a screen cap of my file tree for this Flask app...

Correct me if I'm wrong, but I believe that my flask app module should be named Neuroethics_Behavioral_Task, or the name of the root directory... Also the file experiment.py is the same as app.py in most flask tutorials.

My procfile at first looked like

web: gunicorn experiment:app

This is a snippet of the output produced by the logfile, I'm not sure if I needed to actually include everything.

2020-01-29T21:57:21.732859+00:00 app[web.1]: raise HaltServer(reason, self.APP_LOAD_ERROR)
2020-01-29T21:57:21.732876+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'App failed to load.' 4>
2020-01-29T21:57:21.402514+00:00 app[web.1]: [2020-01-29 21:57:21 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-01-29T21:57:21.404711+00:00 app[web.1]: [2020-01-29 21:57:21 +0000] [4] [INFO] Listening at: http://0.0.0.0:7314 (4)
2020-01-29T21:57:21.404958+00:00 app[web.1]: [2020-01-29 21:57:21 +0000] [4] [INFO] Using worker: sync
2020-01-29T21:57:21.416217+00:00 app[web.1]: [2020-01-29 21:57:21 +0000] [10] [INFO] Booting worker with pid: 10
2020-01-29T21:57:21.431599+00:00 app[web.1]: [2020-01-29 21:57:21 +0000] [11] [INFO] Booting worker with pid: 11
2020-01-29T21:57:21.624070+00:00 app[web.1]: Failed to find attribute 'app' in 'experiment'.
2020-01-29T21:57:21.625302+00:00 app[web.1]: Failed to find attribute 'app' in 'experiment'.
2020-01-29T21:57:21.625425+00:00 app[web.1]: [2020-01-29 21:57:21 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-01-29T21:57:21.626629+00:00 app[web.1]: [2020-01-29 21:57:21 +0000] [11] [INFO] Worker exiting (pid: 11)
2020-01-29T21:57:21.866311+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-29T21:57:21.847634+00:00 heroku[web.1]: Process exited with status 1

So I think the error of interest is Failed to find attribute 'app' in 'experiment'.

I wasn't really sure what to do, so I changed my procfile to the following

web: gunicorn Neuroethics_Behavioral_Task.experiment:app

Trying to deploy this to heroku led to the following in my log file. Again, I'm only posting a snippet.

2020-01-29T22:04:04.569529+00:00 app[web.1]: ModuleNotFoundError: No module named 'Neuroethics_Behavioral_Task'
2020-01-29T22:04:04.569692+00:00 app[web.1]: [2020-01-29 22:04:04 +0000] [11] [INFO] Worker exiting (pid: 11)
2020-01-29T22:04:04.690072+00:00 app[web.1]: [2020-01-29 22:04:04 +0000] [4] [INFO] Shutting down: Master
2020-01-29T22:04:04.690264+00:00 app[web.1]: [2020-01-29 22:04:04 +0000] [4] [INFO] Reason: Worker failed to bo ot.
                 2020-01-29T22:12:52.569782+00:00 heroku[web.1]: State changed from crashed to starting
2020-01-29T22:12:59.200397+00:00 heroku[web.1]: Starting process with command `gunicorn Neuroethics_Behavioral_Task.experiment:app`
2020-01-29T22:13:03.010772+00:00 heroku[web.1]: State changed from starting to crashed
2020-01-29T22:13:02.559827+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-01-29T22:13:02.561033+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [4] [INFO] Listening at: http://0.0.0.0:25539 (4)
2020-01-29T22:13:02.561235+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [4] [INFO] Using worker: sync
2020-01-29T22:13:02.568896+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [10] [INFO] Booting worker with pid: 10
2020-01-29T22:13:02.588814+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [10] [ERROR] Exception in worker process
2020-01-29T22:13:02.588821+00:00 app[web.1]: Traceback (most recent call last):
2020-01-29T22:13:02.588824+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-01-29T22:13:02.588827+00:00 app[web.1]: worker.init_process()
2020-01-29T22:13:02.588829+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-01-29T22:13:02.588831+00:00 app[web.1]: self.load_wsgi()
2020-01-29T22:13:02.588833+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-01-29T22:13:02.588835+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-01-29T22:13:02.588837+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-01-29T22:13:02.588839+00:00 app[web.1]: self.callable = self.load()
2020-01-29T22:13:02.588841+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-01-29T22:13:02.588843+00:00 app[web.1]: return self.load_wsgiapp()
2020-01-29T22:13:02.588845+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-01-29T22:13:02.588847+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-01-29T22:13:02.588849+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-01-29T22:13:02.588851+00:00 app[web.1]: mod = importlib.import_module(module)
2020-01-29T22:13:02.588853+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-01-29T22:13:02.588855+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-01-29T22:13:02.588858+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-01-29T22:13:02.588860+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-01-29T22:13:02.588862+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
2020-01-29T22:13:02.588863+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-01-29T22:13:02.588866+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-01-29T22:13:02.588868+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-01-29T22:13:02.588870+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
2020-01-29T22:13:02.593430+00:00 app[web.1]: ModuleNotFoundError: No module named 'Neuroethics_Behavioral_Task'
2020-01-29T22:13:02.593748+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-01-29T22:13:02.617045+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [11] [INFO] Booting worker with pid: 11
2020-01-29T22:13:02.650580+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [11] [ERROR] Exception in worker process
2020-01-29T22:13:02.650586+00:00 app[web.1]: Traceback (most recent call last):
2020-01-29T22:13:02.650588+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-01-29T22:13:02.650595+00:00 app[web.1]: worker.init_process()
2020-01-29T22:13:02.650597+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-01-29T22:13:02.650599+00:00 app[web.1]: self.load_wsgi()
2020-01-29T22:13:02.650602+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-01-29T22:13:02.650604+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-01-29T22:13:02.650606+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-01-29T22:13:02.650608+00:00 app[web.1]: self.callable = self.load()
2020-01-29T22:13:02.650610+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-01-29T22:13:02.650612+00:00 app[web.1]: return self.load_wsgiapp()
2020-01-29T22:13:02.650614+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-01-29T22:13:02.650616+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-01-29T22:13:02.650618+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-01-29T22:13:02.650620+00:00 app[web.1]: mod = importlib.import_module(module)
2020-01-29T22:13:02.650622+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-01-29T22:13:02.650624+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2020-01-29T22:13:02.650627+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-01-29T22:13:02.650629+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-01-29T22:13:02.650631+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
2020-01-29T22:13:02.650632+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-01-29T22:13:02.650634+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-01-29T22:13:02.650636+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-01-29T22:13:02.650638+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
2020-01-29T22:13:02.650693+00:00 app[web.1]: ModuleNotFoundError: No module named 'Neuroethics_Behavioral_Task'
2020-01-29T22:13:02.651049+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [11] [INFO] Worker exiting (pid: 11)
2020-01-29T22:13:02.831990+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [4] [INFO] Shutting down: Master
2020-01-29T22:13:02.832483+00:00 app[web.1]: [2020-01-29 22:13:02 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-01-29T22:13:02.988239+00:00 heroku[web.1]: Process exited with status 3

The error of interest here is No Module named Neuroethics_Behavioral_Task

At this point I'm done trying to guess how the procfile is supposed to look... could someone explain the syntax (if that's the issue here?)

Edit 2: I changed __init__.py and experiment.py slightly based off of one comment. Right now __init__.py shows the following

import os

from flask import Flask
from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for
)

# app = Flask(__name__, instance_relative_config=True)

def create_app(test_config=None):
    # create and configure the 
    from .experiment import app
    #app = Flask(__name__, instance_relative_config=True) # creates the flask instance
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'Neuroethics_Behavioral_Task.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    # from . import experiment
    # from the current directory, import the variables in the experiment.py 
    app.register_blueprint(experiment.bp)
    app.add_url_rule('/', endpoint='index')



    return app

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

As for experiment.py

import functools

from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for, Flask
)
from werkzeug.security import check_password_hash, generate_password_hash


app = Flask(__name__, instance_relative_config=True) # creates the flask instance

bp= Blueprint('experiment', __name__)

# ... all of my view functions
# ...
# Skipping all the way to main

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

I'm growing more and more suspicious that perhaps the directory tree structure for my project isn't correct.


回答1:


If _init_.py includes the import of your app like this, from .experiment import app, then try web: gunicorn Neuroethics_Behavioral_Task:app



来源:https://stackoverflow.com/questions/59976419/heroku-flask-deployment-failing-either-cant-find-attribute-app-or-cant-fin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!