[英]Deploying Postgres Flask App on Heroku, "gunicorn.errors.AppImportError: Failed to find attribute 'app' in 'app'."
I've been trying to deploy my Flask app to Heroku for over a day, watching/reading several tutorials, and digging through all the stackoverflow questions i could find and still haven't found a solution for my issue.一天多来,我一直在尝试将我的 Flask 应用程序部署到 Heroku,观看/阅读几个教程,并深入研究我能找到的所有 stackoverflow 问题,但仍然没有找到解决我的问题的方法。 When i deploy my app, I get the error "gunicorn.errors.AppImportError: Failed to find attribute 'app' in 'app'."
当我部署我的应用程序时,我收到错误“gunicorn.errors.AppImportError: Failed to find attribute 'app' in 'app'。” I've changed my Procfile many times to get heroku to run my app but i always get the same error.
我已经多次更改我的 Procfile 以让 heroku 运行我的应用程序,但我总是遇到同样的错误。
This is how my app is structured: enter image description here这就是我的应用程序的结构:在此处输入图像描述
My Procfile includes this statement:我的 Procfile 包含以下语句:
web: gunicorn app:app --preload
I am thinking my issues could be how im creating my app, but could not find a solution.我在想我的问题可能是我如何创建我的应用程序,但找不到解决方案。 My main app file looks like this:
我的主应用程序文件如下所示:
from venv import create
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os.path import join, dirname, realpath
from flask_login import LoginManager
db = SQLAlchemy()
def create_app():
app = Flask(__name__, static_folder='static')
app.debug = True
app.config['SECRET_KEY'] = 'abcde12568901fghij'
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
app.config['SQLALCHEMY_DATABASE_URI'] = '[removed for privacy]'
app.config['UPLOAD_DIRECTORY'] = join(dirname(realpath(__file__)), 'uploads/')
app.config['MAX_CONTENT_LENGTH'] = 16*1024*1024 #16mb
app.config['ALLOWED_EXTENSIONS'] = ['.jpg','.jpeg','.png','.gif','.mp4']
db.init_app(app)
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
from models import User
@login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
from auth import auth
app.register_blueprint(auth)
from views import main
app.register_blueprint(main)
return app
if __name__ == '__main__':
db.create_all(app=create_app())
Screenshot of full logs: enter image description here完整日志的屏幕截图:在此处输入图像描述
Any help would be greatly appreciated, thanks all!任何帮助将不胜感激,谢谢大家!
app:app
tells Gunicorn to look for a variable called app
in your app.py
file (strictly speaking, in your app
module), but you don't have a variable with that name. app:app
告诉 Gunicorn 在您的app.py
文件(严格来说,在您的app
模块中)中查找名为app
的变量,但您没有具有该名称的变量。
Instead, you have a function create_app()
that returns your app object.相反,您有一个function
create_app()
返回您的应用程序 object。 Gunicorn allows you to use that directly : Gunicorn允许您直接使用它:
The variable name can also be a function call.
变量名也可以是 function 调用。 In that case the name will be imported from the module, then called to get the application object.
在这种情况下,将从模块中导入名称,然后调用以获取应用程序 object。 This is commonly referred to as the "application factory" pattern.
这通常被称为“应用程序工厂”模式。
Update your Procfile
accordingly:相应地更新您的
Procfile
:
web: gunicorn 'app:create_app()' --preload
Do you have another file in your root directory that is creating the app or is the code above the file that you run to run the app?您的根目录中是否有另一个文件正在创建应用程序,或者是您为运行应用程序而运行的文件上方的代码? This will normally just be a Python file with the name of your project, and this will have something like
app = create_app()
.这通常只是一个带有项目名称的 Python 文件,这将具有类似
app = create_app()
的内容。
Then your Procfile
needs to look for the app in that file.然后您的
Procfile
需要在该文件中查找应用程序。 For example if that file was called mainApp.py
you would then use例如,如果该文件名为
mainApp.py
,那么您将使用
web: gunicorn mainApp:app --preload
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.