1

我的 GF 正在尝试学习 Udacity 的 Web 开发课程,但她遇到了问题。而我解决不了。刚开始必须创建一个在 AppEngine 上运行的“hello world”Python 脚本。

所以,文件:

应用程序.yaml:

application: focus-invention-298
version: 1
runtime: python27
api_version: 1
threadsafe: true

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

你好世界.py:

# -*- coding: utf8 -*-

​import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, Udacity!')

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

但是,当我运行应用程序(通过 GUI 启动器或使用 dev_appserver.py)并在浏览器中打开应用程序时,我收到此错误(在控制台中):

Traceback (most recent call last):
  File "/Users/Kaja/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 196, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/Kaja/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 255, in _LoadHandler
    handler = __import__(path[0])
  File "/Users/Kaja/Documents/udacity/helloworld.py", line 3
    ​import webapp2
    ^
SyntaxError: invalid syntax
INFO     2013-08-05 14:06:00,875 module.py:595] default: "GET / HTTP/1.1" 500 -
ERROR    2013-08-05 14:06:01,012 wsgi.py:219] 
Traceback (most recent call last):
  File "/Users/Kaja/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 196, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/Kaja/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 255, in _LoadHandler
    handler = __import__(path[0])
  File "/Users/Kaja/Documents/udacity/helloworld.py", line 3
    ​import webapp2
    ^
SyntaxError: invalid syntax

我们在 OSX 10.8.4 上,当我在终端中运行 python 时,它告诉我我安装了 2.7.2 版本。AppEngine 启动器(或 SDK)版本为 1.8.2。

任何人?我现在尝试了很多事情都没有成功,以至于我真的不知道该怎么做了(我不是 python 开发人员),我真的想让这件事发挥作用,这样我的 GF 就可以继续学习了 :)

4

1 回答 1

3

语句之前的字节import(unicode 不间断空白字符是主要候选字符)可能会导致这种情况。

检查前 50 个字节左右:

print repr(open('helloworld.py', 'rb').read(50))

例如,如果您看到这样的序列,'\xc2\xa0'则其中包含 UTF-8 编码的不可破坏空格字符。

于 2013-08-05T14:25:40.503 回答