NameError: name 'ask' is not defined

NameError: name 'ask' is not defined

烧瓶有问题请教

@ask.launch 问题

我的 python flask 脚本有问题 运行。我正在使用 python 2.7,错误提示: 文件 "C:\Users\user1\AppData\Local\Continuum\anaconda2\Lib\site-packages\hello_lumion.py",第 13 行,在 @ask.launch 中 NameError: name 'ask' is not defined

import logging
import os 


from flask import request
from flask import Flask

from flask_ask import Ask, statement, request, context, session, question,  version

import requests


@ask.launch
def welcome():
    return statement ('Welcome to Foo')


app = Flask(__name__)

ask= Ask(app,"/")

logging.getLogger("flask_ask").setLevel(logging.DEBUG)

@ask.intent("Hello")

def hello():
    msg= "hello from lumion"
    return statement (msg)


if __name__ == '__main__':
    port = 9000
    app.run(host='0.0.0.0', port=port)
    app.run(debug=True) 

关于如何克服这个问题有什么建议吗?

您在 ask 定义之前调用它。在你的代码中你有

@ask.launch # ask has not been made
def welcome():
    return statement ('Welcome to Foo')


app = Flask(__name__)

ask= Ask(app,"/") # ask gets made here!

您需要重新排序,以便在您调用 ask 时,它已被定义。类似于:

app = Flask(__name__)

ask= Ask(app,"/") # define it first

@ask.launch # now use it
def welcome():
    return statement ('Welcome to Foo')