Google App Engine: .pyファイルに日本語を入れると 500 Server Error

.pyファイルに日本語を入れると 500 Server Error となります。

サンプルプログラム
http://code.google.com/intl/ja/appengine/docs/python/gettingstarted/usingwebapp.htmlのサンプルににコメントを加えたもの)

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
  def get(self):
    # コメント
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Hello, webapp World!')

application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

実行すると、エラーになります。MS932でも、utf-8でもだめでした。

Error: Server Error
The server encountered an error and could not complete your request.

If the problem persists, please report your problem and mention this error message and the query that caused it.

正しくページを取得できる場合、以下のようにブラウザに表示されます。

Hello, webapp World!

対処

" #!-*- coding:utf-8 -*-"を記述することで、対応できるようです。

#!-*- coding:utf-8 -*-
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
  def get(self):
    # コメント
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Hello, webapp World!')

application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

以下のような場所に書いてもだめみたいです。

  def get(self):
    #!-*- coding:utf-8 -*-
    # コメント
    self.response.headers['Content-Type'] = 'text/plain'

感想

日本語に問題があることにたどり着くのに時間がかかりました。ローカルでは動いていたので。