网站地图 联系我们

学习python首选-实战python学习班,专注于python高级工程师培养,python课程:包括Python核心编程技术、全栈开发、网络爬虫、人工智能等。python全栈工程师+提高工作经验,获取免费试听课程!
当前位置: python培训 > python学习 > 常见问题 > 正文

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

来源:肚皮舞 浏览量: 发布日期:20-08-31 10:24:50 调整字体大小【 【所属栏目:常见问题】

  教程地址: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 "", line 1, in

  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改为所在的文件名

标签:
肚皮舞新闻动态News Center

肚皮舞视频

肚皮舞培训课程

联系卡伊丝contact us
联系我们
python培训「零基础入门到精通」python人工智能编程培训班-0798wz.com

咨询热线:18301225663