Flaskアプリをデプロイする際にやったことメモ
実際にやってみた際のメモ。
振り返りなので実際はエラーが出たかもしれないが。
今後は都度メモるようにしましょう。
pyenvのインストール
% brew link autoconf pkg-config % brew install pyenv % brew install pyenv-virtualenv
virtualenvのインストール
% pip install virtualenv
ここまではすんなり完了。
そしてこれはローカルの開発環境を変えない限りは一度やればOKと認識。あってるかな。
Pythonの設定
% echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile % echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile % echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
参考にしたサイトの通り、.zshrcの代わりに.bash_profileとした。
「そのままだと、システムのpythonを使ってしまうのでpyenvのpythonを使うように環境変数を変更します。」らしい。このコマンドの中身はよくわかってなくとりあえずコピペ。
各バージョンのインストール
ここは言われたとおり進めてすんなり。
プログラムの設定
アプリの各プログラムを格納するプロジェクトフォルダを作成する。
$ mkdir (フォルダ名1)
virtualenvコマンドで仮想の環境を生成する。
$ virtualenv (フォルダ名2 上で作ったフォルダ1の下にごっそり仮想環境のフォルダ2ができる) #実行結果 Using base prefix '/anaconda3' New python executable in /Users/yusuke/mikapp02/project/bin/python Installing setuptools, pip, wheel...done.
ここまで終了した段階でフォルダ構成は下のようになる。
. └── project ├── bin │ ├── activate │ ├── activate.csh │ ├── activate.fish │ ├── activate_this.py │ ├── easy_install │ ├── easy_install-3.6 │ ├── pip │ ├── pip3 │ ├── pip3.6 │ ├── python │ ├── python-config │ ├── python3 -> python │ ├── python3.6 -> python │ └── wheel ├── include │ └── python3.6m -> /anaconda3/include/python3.6m ├── lib │ └── python3.6 └── pip-selfcheck.json 6 directories, 15 files
binディレクトリの中のactivateをsource
$ source project/bin/activate #実行結果 (project) YusukenoMacBook-Pro:mikapp02 yusuke$
仮想環境に切り替わる。
Flaskアプリの本体app.pyとtemplatesフォルダをappフォルダにまとめて次の場所に置く。ここじゃないとダメなのかどうか、今のとこよくわからん。
. ├── app │ ├── app.py │ └── templates ├── bin │ ├── activate │ ├── activate.csh │ ├── activate.fish │ ├── activate_this.py │ ├── easy_install │ ├── easy_install-3.6 │ ├── pip │ ├── pip3 │ ├── pip3.6 │ ├── python │ ├── python-config │ ├── python3 -> python │ ├── python3.6 -> python │ └── wheel ├── include │ └── python3.6m -> /anaconda3/include/python3.6m ├── lib │ └── python3.6 └── pip-selfcheck.json
projectフォルダに戻って色々インストールしていく。
戻らなあかんのかな?よくわからん。
runtime.txt, requirements.txt, Procfileの設定をしておく。
herokuにログイン
$ heroku login
heroku上に適当な名前のアプリ名をつけて、pythonを実行することを明示的に示すコマンドを利用。
$ heroku create <app-name> $ heroku buildpacks:set heroku/python
とすればいいらしいのだが、エラーが出てうまくいかなかったので以下を参考に
$ heroku create (アプリ名) --buildpack heroku/python
としたらとりあえずうまくいった。
devcenter.heroku.com
このあとはherokuのガイド通りにやるとうまくいった。
Create a new Git repository
Initialize a git repository in a new or existing directory
$ cd my-project/ $ git init $ heroku git:remote -a mikapp02
Deploy your application
Commit your code to the repository and deploy it to Heroku using Git.
$ git add . $ git commit -am "make it better" $ git push heroku master
Existing Git repository
For existing repositories, simply add the heroku remote
$ heroku git:remote -a mikapp02
次のやり方だとうまくいかないのは何故。
(project) YusukenoMacBook-Pro:app yusuke$ git add -A (project) YusukenoMacBook-Pro:app yusuke$ git commit -m "deploy heroku" [master (root-commit) 59a326a] deploy heroku Committer: Yusuke Mikami <yusuke@YusukenoMacBook-Pro.local> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit your configuration file: git config --global --edit After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 6 files changed, 267 insertions(+) create mode 100644 Procfile create mode 100644 app.py create mode 100644 config.py create mode 100644 requirements.txt create mode 100644 runtime.txt create mode 100644 templates/index.html (project) YusukenoMacBook-Pro:app yusuke$ git push heroku master 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.
DaiさんのFlaskアプリチュートリアルをやってみました
ローカル開発環境と仮想環境の違いもよく理解しないまま進めたもんだからうんざりするほどエラーがでた。ググりながらエラーを一つ一つ潰していったらなんとかできて感動。
エラーの解決方法をちゃんとメモしていったほうが良いとわかってはいたけれど、色々な解決方法を次々に試していったのでそこまでできず。
とりあえずできたので、おさらいしながら理解していく。
これを弄りながら色々試すと本当勉強になるな。
Daiさんありがとう。僕でもできました。
■
$ git push heroku master To https://git.heroku.com/mikapp2.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://git.heroku.com/mikapp2.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
■
git push heroku masterでのエラー。
$ git push heroku master To https://git.heroku.com/mikapp2.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://git.heroku.com/mikapp2.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
■
>||と||<で囲む。>|言語|とすると言語に合わせてハイライトされる。
print("test")