NameError:未定义名称'flask'

时间:2015-04-05 13:08:41

标签: python flask

我在终端中运行这行代码时遇到问题:

$ sudo python app.py 

回溯是:

Traceback (most recent call last):

  File "app.py", line 7, in <module>

   APP = flask.Flask(__name__)

NameError: name 'flask' is not defined

sudo pip install flask

Requirement already satisfied (use --upgrade to upgrade): flask in 

/usr/local/lib/python2.7/dist-packages

Requirement already satisfied (use --upgrade to upgrade): Werkzeug>=0.7 in 

/usr/local/lib/python2.7/dist-packages (from flask)

Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in 

/usr/local/lib/python2.7/dist-packages (from flask)

Requirement already satisfied (use --upgrade to upgrade): 

itsdangerous>=0.21 in /usr/local/lib/python2.7/dist-packages (from flask)

Requirement already satisfied (use --upgrade to upgrade): markupsafe in 

/usr/lib/python2.7/dist-packages (from Jinja2>=2.4->flask)

Cleaning up...

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:6)

如果没有安装烧瓶:

pip install flask

然后

import flask

答案 1 :(得分:2)

就我而言,我已经安装了烧瓶。 我遇到了同样的错误,但这对我有用:

import flask
from flask import Flask

只是发布此内容,以防它可以帮助其他人。 :)

答案 2 :(得分:1)

问题的答案是:

  1. 如果您的导入语句是这样-

    from flask import Flask
    
    app = flask.Flask(name)
    

    然后它将给出错误:

      

    NameError:名称“ flask”未定义

  2. 如果您的导入语句是这样-

    导入烧瓶

    app = Flask(name)

    然后它将给出错误:

      

    NameError:名称“ Flask”未定义

* *上述错误的解决方案-* *

  1. 如果未安装,请通过pip install flask安装flask。
  2. 下面的代码将解决您的问题。

    from flask import Flask
    app = Flask(name)   
    

    OR

    import flask
    app = flask.Flask(name)