Python用Webフレームワーク「Flask」で作成したアプリをHerokuで公開する方法について入門者向けにまとめました。
【Windows編】Flask製アプリをherokuで公開
メールアドレスとパスワードで登録します。
公式ページ(https://devcenter.heroku.com/articles/heroku-cli#download-and-install)からHeroku CLIをインストールします。
ターミナルを開いて以下のコマンドを実行します。
heroku login
ウィンドウが開いたら[LOGIN]ボタンをクリックします。
以下のコマンドを実行します。
heroku create [アプリ名]
※他と被らないアプリ名をつけます。
heroku git:remote -a [アプリ名]
アプリファイル(main.py)と、設定ファイル(runtime.txt、requirements.txt、Procfile)を作成し、以下のディレクトリ構造で保存します。
├── myapp ├── main.py ├── runtime.txt ├── requirements.txt └── Procfile
runtime.txt
python-3.8.1
requirements.txt
Flask==1.1.1
main.py
from flask import Flask import os app = Flask(__name__) @app.route("/") def hello(): return "Hello, Heroku" if __name__ == "__main__": port = int(os.getenv("PORT", 5000)) app.run(host="0.0.0.0", port=port)
Procfile
web: python main.py
作業ディレクトリまで移動し、以下のコマンドを実行します。
git init git add . git commit -m "first commit" git push heroku master
以下のURLにアクセスしてアプリが表示されたら成功です。
https://アプリ名.herokuapp.com/
【補足】エラー対策集
デプロイ時にエラーが出た場合については下記事にまとめています。

【Heroku】デプロイしたときのよくあるエラー対策集
Herokuでデプロイしたときのよくあるエラー対策集をついてまとめました。
コメント