部署python程序到google app engine
一、gae的部署需要python2.X。于是我的電腦中就不得不又安裝了一個python2.7版。為了保證gae訪問的是正確的python版本,需要打開Google App Engine Launcher后,選擇菜單Edit->Preference修改python解釋器的路徑。
二、部署demo應用
選擇菜單File->Add demo Application->python->guestbook可以創建一個demo應用,通過運行demo應用可以了解GAE的開發方法。
可惜的是,初次部署完guestbook后,運行會出錯。打開Logs會提示錯誤信息:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)
Google搜索后發現原來是python 2.7的庫文件Lib/mimetypes.py存在bug.解決辦法參照
這里修改。行前標記+的是需要新增的內容,行前標記-的是要刪除的內容。保存退出后,再運行guestbook就沒問題了。注意+from itertools import count這一行不要漏掉了。

三、本地調試Google App Engine應用
錯誤"from google.appengine.api import urlfetch
ImportError: No module named google.appengine.api"。這是因為環境變量沒有設置正確。
在環境變量增加一條PythonPath=“your google app engine path”

錯誤No api proxy found for service "urlfetch"
本地調試urlfetch調用時,會遇到這種錯誤。解決辦法
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import urlfetch_stub
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
apiproxy_stub_map.apiproxy.RegisterStub('urlfetch', urlfetch_stub.URLFetchServiceStub())
如果需要調試其他gae API(比如mail,datastore),可以添加以下代碼
from google.appengine.api import datastore_file_stub
from google.appengine.api import mail_stub
from google3.apphosting.api import user_service_stub
apiproxy_stub_map.apiproxy.RegisterStub('user',
user_service_stub.UserServiceStub())
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3',
datastore_file_stub.DatastoreFileStub('your_app_id', '/dev/null', '/
dev/null'))
apiproxy_stub_map.apiproxy.RegisterStub('mail',
mail_stub.MailServiceStub())
調試gae應用程序,也可以考慮使用在線python調試環境http://py-ide-online.appspot.com/
或者使用Google提供的unittest功能。可以參考官網文檔
這里。
python ImportError: No module named yaml錯誤
我的運行環境是win7 64位,本想使用easy_install pyyaml來安裝,結果沒找到。
只好從
官網下載 pyyaml源碼,然后執行:python setup.py --without-libyaml install重新編譯并安裝。