【发布时间】:2021-04-07 09:17:03
【问题描述】:
I found a similar question here 但该解决方案对我不起作用。
我按照官方教程编写了一个 Flask 应用程序。我正在尝试使用 Google App Engine 部署它。我用过蓝图。我的应用程序的目录结构如下所示:
app
│ .gcloudignore
│ app.yaml
│ main.py
│ requirements.txt
│ __init__.py
│
├───googleutils
│ │ utils.py
│
├───home
│ │ home.py
│ │
│ ├───static
│ ├───templates
│ │ home.html
│
├───models
│ │ Message.py
│ │ User.py
│
├───profile
│ │ profile.py
│ │
│ ├───static
│ ├───templates
│ │ edit_message.html
│ │ profile.html
│
├───sessions
│ │ sessions.py
│ │
│ ├───templates
│ │ login.html
│ │ logout.html
│ │ register.html
│
├───static
│ style.css
│
├───templates
│ layout.html
│ wrapper.html
在我的应用程序目录中,我有我的app.yaml 和requirements.txt。我在我的 venv 中使用了pip freeze,然后把它吐出的所有东西都扔进了requirements.txt。
当前的 Flask 教程指导您通过命令行使用 flask run 启动您的应用程序。因此,我的申请从__init__.py开始:
from flask import Flask
def create_app():
app = Flask(__name__, instance_relative_config=True)
# Application imports
from .home import home
from .sessions import sessions
from .profile import profile
# Blueprints
app.register_blueprint(home.home_bp)
app.register_blueprint(sessions.sessions_bp)
app.register_blueprint(profile.profile_bp)
return app
但是,正如我在上面链接的问题中所述,gcloud 会查找 main.py,因此我将其添加到我的根目录中:
from app import create_app
app = create_app()
目前我只是想加载我的索引,它在home.py 中定义:
@home_bp.route('/', methods=['GET', 'POST'])
def home():
...
这是我的 app.yaml:
runtime: python39
entrypoint: gunicorn "app:create_app()"
handlers:
- url: /static
static_dir: static
- url: /.*
script: auto
说实话我不知道怎么写这个文件,特别是对于有变量的目录,比如profile.py中定义的这个:
@profile_bp.route('/profile/<string:user_id>', methods=['GET', 'POST'])
def profile(user_id):
...
当我运行 gcloud app deploy 时,当我在浏览器中查看我的网站时收到 500 服务器错误,“服务器遇到错误,无法完成您的请求。”。
我做错了什么?
【问题讨论】:
-
我不熟悉
app.yaml的作用,所以这可能不适用,但看起来您的main.py缺少if __name__ == '__main__': app.run()。 (这可能也没什么区别,但我个人总是将main.py放在根文件夹中,与app目录位于同一级别,而不是在其中) -
app.yaml 文件是部署应用到 Google App Engine 所必需的。不幸的是,添加这些行并没有解决我的问题。
-
您是否查看了日志以查看 500 错误背后的原因?您还应该能够在日志中看到回溯,以了解错误发生在代码中的哪个位置。
-
我到处寻找日志,但没有找到。它在哪里?
-
你必须去console.cloud.google.com,选择你的项目,然后在菜单上(在你的左边),应该有一个日志条目。在子菜单中使用
Legacy Log Viewer,这样更容易理解
标签: python google-app-engine flask