gpt4 book ai didi

python - 简单的 Python 导入出现语法错误

转载 作者:行者123 更新时间:2023-11-30 23:34:31 25 4
gpt4 key购买 nike

我的女朋友正在尝试遵循 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 就可以继续学习:)

最佳答案

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

检查前 50 个字节左右:

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

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

关于python - 简单的 Python 导入出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18060330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com