GAE + python + Djangoでハマった話


経緯

ローカルで動くのに、GAEにデプロイしたら下記のようなエラー表示が。。。

Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.

ログの確認

エラーログを確認するため、下記のコマンドで状況を見るとどうやらmoduleがうまく探せていないようでした。ここで'main'ってなんだ、という罠に陥りました。

c:\Users\test>gcloud app logs tail -s default
…
 File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
 ModuleNotFoundError: No module named 'main'

結果

app.yamlの中に下記の文書を加えるだけで解決した。

app.yaml
runtime: python37
entrypoint: gunicorn -b :8080 [プロジェクト名].wsgi ←←←ここ!!!

handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
  static_dir: static/

# This handler routes all requests not caught above to your main app. It is
# required when static routes are defined, but can be omitted (along with
# the entire handlers section) when there are no static files defined.
- url: /.*
  script: auto

最後に

GAE djangoのチュートリアルでは記述がないため、どうしたらいいかわからず
ログの出し方を学んでようやくたどり着きました。
このエラー解決で誰かの助けになれば幸いです。

参考URL

○App Engine スタンダード環境での Django の実行
(GAE django チュートリアル)
https://cloud.google.com/python/django/appengine?hl=ja
○Python 3 Django on App Engine Standard: App Fails to Start
(検索で引っかかった解決策)
https://stackoverflow.com/questions/52416588/python-3-django-on-app-engine-standard-app-fails-to-start
○Python 3 ランタイム環境
(app.yamlの設定について)
https://cloud.google.com/appengine/docs/standard/python3/runtime