【Python/Flask】作成したアプリをHerokuで公開する方法(Windows、Mac編)

Python用Webフレームワーク「Flask」で作成したアプリをHerokuで公開する方法について入門者向けにまとめました。

スポンサーリンク

【Flask】作成したWebアプリをherokuで公開

Herokuとは、開発したWebアプリケーションを簡単にインターネット上に公開できるサービスです。
Python/Java/Go/PHP/Node.JS/Ruby on Railsに対応しており、PythonとFlaskで作成したWebアプリも簡単に公開できます。

そして、なんとHerokuには「無料プラン」があり、趣味レベルであれば無料プランでもそれなりに利用できます。

【無料プランの制限】
① 公開したアプリケーションの合計稼働時間が、クレジットカード登録なしで550時間/月、登録した場合は1000時間/月まで利用可能
→1つのアプリケーションを公開し続けるだけなら24時間×31日=744時間なので、無料プランだけでも可能(要クレジットカード登録)

② Webアプリケーションに30分以上アクセスが無いとスリープ状態になる(スリープ状態の間は稼働時間にカウントされません)
→一度スリープ状態になると、アクセス時にWebアプリの起動に10秒くらい時間がかかります。30分以内に1度、WebアプリケーションへアクセスするBOTを使えば回避可能。

③ Webアプリのデータ容量は圧縮状態で最大100MBまで利用可能。

④ データベースはPostgreSQLのみ利用でき、容量は5MBまで。

動画解説

本ページの内容は以下動画でも解説しています。

スポンサーリンク

【Heroku】デプロイ手順

● 以下リンクから「git」をインストールします。
https://git-scm.com/

● 以下リンクからメールアドレスなど必要事項を記入してHerokuのアカウントを作成します。
https://signup.heroku.com

● 以下リンクから登録したメールアドレスとパスワードでログインします。
https://id.heroku.com/login

● 公式ページ(https://devcenter.heroku.com/articles/heroku-cli#download-and-install)からHeroku CLIをインストールします。

【Windowsの場合】
上記ページの[64 bit Installer]もしくは[32 bit Installer]をクリックしてインストーラをダウンロードし、実行してインストールします。

【Macの場合】
上記ページのとおり、ターミナルで以下のコマンドを実行してインストールします。

brew tap heroku/brew && brew install heroku

※MacにHomebrewをインストールしていない場合は、以下記事を参考にインストールしてください。

【Mac】homebrewをインストールする方法
homebrew(読み方:ホームブルー)をMacにインストールする方法についてまとめました。

● コマンドプロンプト(Macならターミナル)を開いて以下のコマンドを実行します。

heroku login

●「heroku: Press any key to open up the browser to login or q to exit: 」と表示されるので、適当なキーを押します。

● 規定のブラウザが自動で起動してHerokuのログインページが表示されるので、[LOGIN]ボタンをクリックします。

● 以下のコマンドを実行してアプリの公開準備をします。

heroku create [アプリ名]

※他の人が公開しているアプリ名と被らないものをつけます。被っていなければ以下のように表示されます。

Creating ⬢ testapp1475... done
・・・https://testapp1475.herokuapp.com/ | https://git.heroku.com/testapp1475.git

● アプリファイル(main.py)と、設定ファイル(runtime.txt、requirements.txt、Procfile)を作成し、以下のディレクトリ構造で保存します。

├── myapp
    ├── app.py
    ├── runtime.txt
    ├── requirements.txt
   └── Procfile

サンプルファイル一式:https://github.com/nishizumi-lab/sample/tree/master/python/flask/06_heroku

【app.py】


※ ポート番号は5000、IPアドレスは「0.0.0.0」にします。

【runtime.txt】


※使用するPythonのバージョンの応じて適宜数値を変更してください。

【requirements.txt】


※使用するモジュールのバージョンの応じて適宜数値を変更してください。他のモジュールも使用する場合は、同じように2行目移行に1行ずつ記述します。

【Procfile】


● コマンドプロンプト(Macならターミナル)で作業ディレクトリ(myapp)まで移動し、以下のコマンドを順に実行します。

heroku git:remote -a [アプリ名]
git init
git add .
git commit -m "release"
git push heroku master

※「Error: Git must be installed to use the Heroku CLI. See instructions here: https://git-scm.com」と表示された場合、「git」をインストールします。
※「release」はコメントなので任意のものでかまいません。

● 以下のように表示されたらデプロイ成功です。

Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (7/7), 1000 bytes | 1000.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Python app detected
remote: -----> Using Python version specified in runtime.txt
remote:  !     Python has released a security update! Please consider upgrading to python-3.9.6
remote:        Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Installing python-3.9.1
remote: -----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote:        Collecting Flask==1.1.1
remote:          Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
remote:        Collecting Werkzeug>=0.15
remote:          Downloading Werkzeug-2.0.1-py3-none-any.whl (288 kB)
remote:        Collecting Jinja2>=2.10.1
remote:          Downloading Jinja2-3.0.1-py3-none-any.whl (133 kB)
remote:        Collecting click>=5.1
remote:          Downloading click-8.0.1-py3-none-any.whl (97 kB)
remote:        Collecting itsdangerous>=0.24
remote:          Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
remote:        Collecting MarkupSafe>=2.0
remote:          Downloading MarkupSafe-2.0.1-cp39-cp39-manylinux2010_x86_64.whl (30 kB)
remote:        Installing collected packages: Werkzeug, MarkupSafe, Jinja2, click, itsdangerous, Flask
remote:        Successfully installed Flask-1.1.1 Jinja2-3.0.1 MarkupSafe-2.0.1 Werkzeug-2.0.1 click-8.0.1 itsdangerous-2.0.1
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 52.4M
remote: -----> Launching...
remote:        Released v3
remote:        https://test1475.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/test1475.git
 * [new branch]      master -> master

※以下のエラーが出た場合は「git remote add heroku https://git.heroku.com/アプリ名.git」を実行します。

fatal: 'heroku' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

● 以下のコマンドを実行するか、「https://アプリ名.herokuapp.com/」にブラウザでアクセスすると、デプロイしたアプリが表示されます。

heroku open

(例)https://app1475.herokuapp.com/

● アプリを更新するには、ファイルの修正後に以下のコマンドを順に実行します。

heroku git:remote -a [アプリ名]
git commit -m "update"
git remote add heroku https://git.heroku.com/アプリ名.git
git push heroku master 

● アプリをHeroku上から削除したい場合は、以下のコマンドを順に実行します。

heroku apps:destroy --app [アプリ名] --confirm [アプリ名]

※アプリが削除され、リモートリポジトリの設定も削除されます。
※上の方法以外に「https://id.heroku.com/login」からログインし、削除したいアプリを選択し、「Settings」タブの一番下に、アプリの削除の項目があるので「Delete app…」ボタンを選択して、確認のためにアプリ名を入力して、実行するとHeroku上からアプリを削除できます。

スポンサーリンク

【その他】herokuのコマンド集

● ログアウト

heroku logout

● ログの確認

heroku logs

● ログの確認(リアルタイム)

heroku logs --tail

● アプリのステータスを確認

heroku ps

● Herokuに作成したアプリを一覧表示

heroku list

● コマンドを実行

heroku run "コマンド"

● アプリ名を指定してコマンドを実行

heroku コマンド --app アプリ名
スポンサーリンク

【補足】エラー対策集

デプロイ時にエラーが出た場合については下記事にまとめています。

エラー名・詳細リンク
1 ‘heroku’ does not appear to be a git repository
2 Do not authenticate with username and password using git.
3 error: failed to push some refs to
4 Please tell me who you are
【Heroku】デプロイしたときのよくあるエラー対策集
Herokuでデプロイしたときのよくあるエラー対策集をついてまとめました。
Python
スポンサーリンク

コメント