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

三、本地調(diào)試Google App Engine應(yīng)用
錯(cuò)誤"from google.appengine.api import urlfetch
ImportError: No module named google.appengine.api"。這是因?yàn)榄h(huán)境變量沒(méi)有設(shè)置正確。
在環(huán)境變量增加一條PythonPath=“your google app engine path”

錯(cuò)誤No api proxy found for service "urlfetch"
本地調(diào)試urlfetch調(diào)用時(shí),會(huì)遇到這種錯(cuò)誤。解決辦法
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())
如果需要調(diào)試其他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())
調(diào)試gae應(yīng)用程序,也可以考慮使用在線python調(diào)試環(huán)境http://py-ide-online.appspot.com/
或者使用Google提供的unittest功能。可以參考官網(wǎng)文檔
這里。
python ImportError: No module named yaml錯(cuò)誤
我的運(yùn)行環(huán)境是win7 64位,本想使用easy_install pyyaml來(lái)安裝,結(jié)果沒(méi)找到。
只好從
官網(wǎng)下載 pyyaml源碼,然后執(zhí)行:python setup.py --without-libyaml install重新編譯并安裝。