2008年9月7日日曜日

eclipse + PyDev で Google App Engine アプリを開発

1. Pydev プロジェクトを作成


2. Google App Engine のライブラリをパスに追加
プロジェクトを右クリック
Properties -> PyDev -PYTHONPATH の External Source Folders の
「Add Source Folder」をクリックして、以下を追加。

/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/yaml


3. app.yaml を作成
src フォルダに app.yaml を作成。
application: sample
version: 1
runtime: python
api_version: 1

handlers:
- url: .*
script: main.py


4. main.py を作成
src フォルダに main.py を作成。
import wsgiref.handlers
from google.appengine.ext import webapp

class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write('Hello world!')

def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
main()


5. プログラムを実行(エラーを発生させる)
main.py を右クリックして、Run As -> Python Run を選択。
アプリケーションの実行エラーが発生します。

6. アプリケーション実行の設定を変更
エラー後、main.py を右クリックして、Run As -> Open Run Dialog を選択。

・Main タブの Main Module に以下を入力。
/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/dev_appserver.py


・Arguments タブの Program arguments に以下を入力。
${project_loc}/src
--port=8080


7. アプリケーションを起動
main.py を右クリックして、Run As -> Python Run とすると、アプリケーションが起動します。
ブラウザで、http://localhost:8080/ にアクセスすると、Hello World とブラウザに表示されます。

デバッグについても、5,6,7 を Debug について実施すると、
ブレークポイントなども利用することができとても便利なのです。

0 件のコメント: