玩蛇网提供最新Python编程技术信息以及Python资源下载!

Flask教程效果做不出来,谁来帮帮我NameError

教程地址:http://docs.jinkan.org/docs/flask/tutorial/dbinit.html#tutorial-dbinit

# all the imports                                          
import sqlite3

from flask import Flask, request, session, g, redirect, url    _for, abort, render_template, flash
# configuration
DATABASE = '/tmp/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

#create our little application 
app = Flask(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)

def connect_db():
    return sqlite3.connect(app.config['DATABASE'])

def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql') as f:
            db.cursor().executescript(f.read())
        db.commit()
if __name__ == '__main__':
    app.run()

在Python shell 里创建数据库这一步报如下错误:

>>> init_db()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "flaskr.py", line 18, in init_db
    with closing(connect_db()) as db:
NameError: global name 'closing' is not defined

from future import with_statement
from contextlib import closing

这两个导入你没做吧?

line2 直接改为from flask import * 加上import os
DATABASE = '/tmp/flaskr.db' 如果是win把tmp改为所在的文件名

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd19712.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月22日 - 08时29分47秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules