使用简单的Python导入语法错误

时间:2013-08-05 14:10:31

标签: python google-app-engine python-2.7

我的GF正试图关注Udacity's Web Development course,但她遇到了问题。我无法解决它。刚刚开始时,必须创建一个在AppEngine上运行的“hello world”Python脚本。

所以,文件:

的app.yaml:

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

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

helloworld.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可以继续学习:)

1 个答案:

答案 0 :(得分:3)

import语句之前有字节(unicode非破坏空白字符是主要候选者)可能导致此问题。

检查前50个字节左右:

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

如果您看到类似'\xc2\xa0'的序列,那么您就会在那里使用UTF-8编码的不可破坏空格字符。