LoginSignup
9
17

More than 3 years have passed since last update.

【Heroku】WindowsでHerokuを使ってPythonアプリをデプロイするメモ【Python】

Last updated at Posted at 2019-12-03

Herokuで無料でアプリを公開したい!

そんなときに見直すメモです。

Herokuに登録・インストール

いつものメールアドレスとパスワードで登録します。
その次にここでHeroku CLIをインストール【https://devcenter.heroku.com/articles/heroku-cli#download-and-install
Windowsの64ビットをダウンロードして、実行するとインストーラーが開くので、そのままインストールする。

ここまで出来たら、ターミナルを開いて

heroku login

で、エンターキーを押す
すると、ウィンドウが開くので、『LOGIN』ボタンを押す。

それが完了したら、
heroku create <アプリケーション名>
今回は、testappxxx01として作ります。※ほかの人と被らない名前にしてください。

heroku create testappxxx01

そしたら、リモートで接続します。
heroku git:remote -a <アプリケーション名>
作業用ディレクトリに移動し、

git init
heroku git:remote -a testappxxx01

次に設定ファイルのruntime.txtrequirements.txtを作成する

runtime.txt
python-3.7.3
requirements.txt
Flask==1.0.2

ここまで出来たら、今度はソースコードを書きます。

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ファイルを作成します。
ファイル保存の際には拡張子を付けず、Procfileと先頭のPを大文字にして保存してください

Procfile
web: python main.py

ここまで出来たらあともう一歩!!
ディレクトリの構成は、

├── 作業ディレクトリ
    ├── main.py
    ├── runtime.txt
    ├── requirements.txt
   └── Procfile

あとは、魔法の4コマンドを打つだけです。

git init
git add .
git commit -m "first commit"
git push heroku master

デプロイの確認

https://testappxxx01.herokuapp.com/
つまり、https://アプリ名.herokuapp.com/ で確認できます。
お疲れ様でした。 

9
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
17