Google App Engine for Pythonの開発環境をUbuntu 10.04に構築する

Google App EnginePython版の開発環境をUbuntu 10.04に構築した際の手順をメモしておきます。
ついでに、Hello WorldをGAEにデプロイするまでの手順も載せておきました。

Python 2.5のインストール

Pythonのバージョンは2.5です。Ubuntuの最近のバージョンのデフォルトは2.6なので、
Python 2.5を以下の手順でインストールします。
まず、Synapticのリポジトリを追加します。
以下の2行を/etc/apt/sources.listに追加するか、Synapticパッケージマネージャで追加してください。

$ sudo vi /etc/apt/sources.list
deb http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu lucid main
deb-src http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu lucid main

apt-getでPython2.5をインストールします。

$ sudo apt-get update
$ sudo apt-get install python2.5

10.04では、apt-getでインストールした場合は、デフォルトのpythonは2.6のまま上書きされることはありませんでしたので、もとの環境に影響はなさそうです。

参考: http://www.codigomanso.com/en/2010/05/google-app-engine-en-ubuntu-10-4-lucid-lynx/

また、apt-getでインストールした場合、SSLの設定が含まれていません。アプリケーションをアップロードする際にSSL通信が必要な場合は、以下のサイトの手順にしたがってPythonコンパイルしてインストールする必要があるようです。
http://thedarren.blogspot.com/2010/11/install-appengine-python-sdk-on-ubuntu.html

Google App Engine SDKのインストール

次にGoogle App Engine SDKをインストールします。
Google App Engine SDK for Pythonを以下のダウンロードサイトからダウンロードします。
http://code.google.com/intl/ja/appengine/downloads.html#Download_the_Google_App_Engine_SDK
Linux用のSDKgoogle_appengine_1.3.8.zip(2010年11月28日現在)です。

ダウンロード後、解凍して、解凍したディレクトリに移動しておきます。

$ wget http://googleappengine.googlecode.com/files/google_appengine_1.3.8.zip
$ unzip google_appengine_1.3.8.zip
$ cd google_appengine

次に、ローカルでHello Worldを実行して、それをGAEにデプロイするところまでを説明します。
この辺はGoogleのドキュメントにも詳しい説明が在るのでわざわざここに書く必要も無いのですが、せっかくなので一通りコマンドをメモしておきます。

ローカルでHelloWorld

helloworld用のディレクトリを作ります。

$ mkdir helloworld
$cd helloworld

リクエストを実行するファイルを作成します。

$ vi helloworld.py
print 'Content-Type: text/plain'
print ''
print 'Hello, world!'

アプリケーションの設定ファイルを作成します。

$ vi app.yaml
application: helloworld
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: helloworld.py

先ほど解凍したgoogle_appsengineディレクトリに戻って、サーバーを起動します。

$ ..
$ python2.5 ./dev_appserver.py helloworld/
hrendoh@hrendoh-laptop:~/google_appengine$ python2.5 dev_appserver.py helloworld/
INFO     2010-11-28 03:24:51,195 appengine_rpc.py:153] Server: appengine.google.com
Allow dev_appserver to check for updates on startup? (Y/n): 
(Yを選択しました)
dev_appserver will check for updates on startup.  To change this setting, edit /home/hrendoh/.appcfg_nag
INFO     2010-11-28 03:26:11,871 appcfg.py:414] Checking for updates to the SDK.
INFO     2010-11-28 03:26:12,966 appcfg.py:428] The SDK is up to date.
WARNING  2010-11-28 03:26:12,966 datastore_file_stub.py:818] Could not read datastore data from /tmp/dev_appserver.datastore
WARNING  2010-11-28 03:26:12,975 dev_appserver.py:3663] Could not initialize images API; you are likely missing the Python "PIL" module. ImportError: No module named _imaging
INFO     2010-11-28 03:26:12,983 dev_appserver_main.py:443] Running application helloworld on port 8080: http://localhost:8080

ブラウザでlocalhost:8080にアクセスして「Hello World!」と表示されれば起動成功です。

この内容は、以下のGoogleの公式ドキュメントに記載されています。
http://code.google.com/intl/ja/appengine/docs/python/gettingstarted/helloworld.html

GAEにデプロイする

http://appengine.google.com/にアクセスして、デプロイ用のアプリケーションを用意します。
まず、[Create an Application] ボタンをクリックします。

アプリケーションIDを指定して[Create Application]ボタンをクリックします。
ここでは、アプリケーションIDを"helloworld-hrendoh"にしました。

以下のページが表示されれば登録終了です。

登録したアプリケーションIDをapp.yamlのapplicationに指定します。に指定します。

$ cd helloworld
$ vi app.yaml
application: helloworld-hrendoh
version: 1
runtime: python
api_version: 1

handlers:
- url: /.*
  script: helloworld.py

再度、解凍したgoogle_appsengineディレクトリに戻って、アップロードのコマンドを実行します。

$ cd ..
$ python2.5 ./appcfg.py update helloworld/
Application: helloworld-hrendoh; version: 1.
Server: appengine.google.com.
Scanning files on local disk.
Initiating update.
2010-11-28 12:54:21,024 WARNING appengine_rpc.py:405 ssl module not found.
Without the ssl module, the identity of the remote host cannot be verified, and
connections may NOT be secure. To fix this, please install the ssl module from
http://pypi.python.org/pypi/ssl .
To learn more, see http://code.google.com/appengine/kb/general.html#rpcssl . 
Email: hrendoh@gmail.com

Password for hrendoh@gmail.com: xxxx

Cloning 2 application files.
Uploading 1 files and blobs.
Uploaded 1 files and blobs
Precompilation starting.
Precompilation completed.
Deploying new version.
Checking if new version is ready to serve.
Will check again in 1 seconds.
Checking if new version is ready to serve.
Will check again in 2 seconds.
Checking if new version is ready to serve.
Closing update: new version is ready to start serving.

この内容は、以下のGoogleの公式ドキュメントに記載されています。
http://code.google.com/intl/ja/appengine/docs/python/gettingstarted/uploading.html

SSLのエラーについて

「python2.5のインストール」のところでも書きましたが、apt-getでインストールすると、SSLサポートなしのPythonがインストールされてしまいます。
以下のGoogle codeのエントリにもありますが、実際にはPythonSSL付きでコンパイルしてインストールする必要があります。
http://code.google.com/p/googleappengine/issues/detail?id=757#c51

Python2.5をコンパイルしてインストールする方法は以下のサイトが良くまとまっているので参考にしてみてください。
http://thedarren.blogspot.com/2010/11/install-appengine-python-sdk-on-ubuntu.html

また、標準でインストールされている2.6はSSLサポートがあるPythonがインストールされています。appcfg.pyはPython2.6でも問題なく実行できたので、実際には自分はアップロードするときのみ2.6を使うようにしています。