我正在关注这个例子,在底部它有一些代码 https://cloud.google.com/prediction/docs/developer-guide
我正在使用Flask而不是webapp2,我的代码如下所示:
# [START app]
import logging
from oauth2client.appengine import AppAssertionCredentials
from flask import Flask
import httplib2, webapp2
from oauth2client.appengine import AppAssertionCredentials
from apiclient.discovery import build
http = AppAssertionCredentials('https://www.googleapis.com/auth/prediction').authorize(httplib2.Http())
service = build('prediction', 'v1.6', http=http)
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World1!'
@app.route('/add')
def something():
class MakePrediction():
def get(self):
result = service.hostedmodels().predict(project=PROJECT-NAME, hostedModelName=PROJECT-ID, body={'input' {'csvInstance': ['hello']}}).execute()
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Result: ' + repr(result))
@app.errorhandler(500)
def server_error(e):
# Log the error and stacktrace.
logging.exception('An error occurred during a request.')
return 'An internal error occurred.', 500
# [END app]
我一直收到错误:
File "/Users/morganallen/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/morganallen/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Users/morganallen/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/morganallen/Desktop/project/flask_app_engine/main.py", line 24
result = service.hostedmodels().predict(project='linear-yen-140912', hostedModelName='language-identifier', body={'input' {'csvInstance': ['hello']}}).execute()
^
SyntaxError: invalid syntax
我做错了什么?
答案 0 :(得分:3)
第24行缺少一个冒号,小胡萝卜指向堆栈跟踪:
File "/Users/morganallen/Desktop/project/flask_app_engine/main.py", line 24
result = service.hostedmodels().predict(project='linear-yen-140912', hostedModelName='language-identifier', body={'input' {'csvInstance': ['hello']}}).execute()
^
所以这里的解决方案是改变这个:
body={'input' {'csvInstance': ['hello']}}).execute()
^
对此:
body={'input' : {'csvInstance': ['hello']}}).execute()
^
这应解决语法错误。
SyntaxError: invalid syntax
每当您收到错误时,请不要忽略编译器吐出的所有行。它通常会告诉您问题的确切行,特别是在像这样的简单语法错误的情况下。