LoginSignup
5
5

More than 5 years have passed since last update.

herokuでpython+django+scikit-learn+mecab (2)

Posted at

前回に引き続き、herokuでpythonアプリを作るためのメモ
今回は開発する上で引っかかったエラーを中心にまとめる。

参照元:
https://gist.github.com/konitter/5370904
http://voluntas.hatenablog.com/entry/20110919/1316426034
http://qiita.com/nakazye/items/48651e39f07da82fe79e

herokuのインストール

heroku本体は,

$ sudo gem install heroku

でインストールできるが,早速エラーが・・・

Operation not permitted - /usr/bin/…/

インストール先のフォルダに権限がないらしい、インストールフォルダを変更することで解決

$ sudo gem install -n /usr/local/bin heroku

(参照元:http://qiita.com/AcaiBowl/items/4bb4708de03e6ee14a4a)

virtualenv

開発用のフォルダの作成

$ mkdir heroku-django
$ cd heroku-django

作成したフォルダ内で、virtualenvを展開。herokuではvirtualenvで開発することが推奨!!(必要なライブラリを最小限にするため)

$ virtualenv --no-site-packages .
$ source bin/activate

必要なパッケージのインストール

$ pip install django
$ env ARCHFLAGS="-arch i386 -arch x86_64" bin/pip install psycopg2
$ pip install gunicorn
  • django: python用のMVCフレームワーク
  • psycopg: pythonからpostgreSQLを使うためのパッケージ、herokuのデータベースはpostgreなため、これが必要
  • gunicorn: よくわかってないが、Apache + mod_wsgiみたいなもの??

heroku_djangoディレクトリで、インストールしたライブラリをrequirements.txtに記録

$ pip freeze > requirements.txt

Procfileの作成

Procfileをheroku_djangoディレクトリ上に作成(gunicornをheroku上で使用するために必要)
ここがなかなか動かなくてつまづいたが

Procfile
web: gunicorn --pythonpath './mysite' mysite.wsgi --log-file -

の1行でおっけー。
ポイントはpythonpathを指定すること

ここからは通常のdjnago開発と同様
http://eiry.bitbucket.org/
にわかりやすくまとめられている。

.gitignoreファイルをheroku_djangoディレクトリに作成
(いらないファイルをpushしないため)

.gitignore
bin/
include/
lib/
*.pyc

デプロイ

デプロイする時,

$ git push heroku master

エラー発生
Error while running '$ python mysite/manage.py collectstatic --noinput'.
heroku上でstaticフォルダが作成できないよというエラー、setting.pyに以下の3行を追加することで解決

setting.py
PROJECT_DIR  = os.path.dirname(__file__)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

staticフォルダはディレクトリフォルダのすぐ下に作ってくださいねという命令

動作チェック

デプロイする前に

$ python manage.py runserver

もしくは,

$ foreman start

でアプリが動くか確認しましょう。

bootstrap

bootstrapを使用する時の注意
runserverではうまく表示されるが、foreman startではうまくcss読み込めないエラー
http://stackoverflow.com/questions/16170030/django-serves-static-files-with-runserver-but-not-with-foreman
がピンポイント。

$ pip install whitenoise

が必要。

5
5
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
5
5