Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 第'条;模块";flaskr.db';没有属性';初始化应用程序'&引用;_Python_Python 3.x_Sqlite_Flask - Fatal编程技术网

Python 第'条;模块";flaskr.db';没有属性';初始化应用程序'&引用;

Python 第'条;模块";flaskr.db';没有属性';初始化应用程序'&引用;,python,python-3.x,sqlite,flask,Python,Python 3.x,Sqlite,Flask,我是flask的新手,我正在学习官方教程,我刚刚设置了我的sqlitedb和模板。问题是当我在设置venv和env变量后运行flask run时。它给我这个错误输出- p.S-flask learn是我的venv(ik很奇怪,我以后会将其设置为venv) 我猜问题出在\uuuu init\uuuu.py或db.py模块上,但我完全按照文档所说的做了。我正在从后端文件夹(下面的目录结构)运行这些脚本 我猜你可能会对目录结构感兴趣,就在这里- backend | ├───flaskr │ ├──

我是flask的新手,我正在学习官方教程,我刚刚设置了我的
sqlite
db和模板。问题是当我在设置venv和env变量后运行
flask run
时。它给我这个错误输出-

p.S-
flask learn
是我的venv(ik很奇怪,我以后会将其设置为
venv

我猜问题出在
\uuuu init\uuuu.py
db.py
模块上,但我完全按照文档所说的做了。我正在从
后端
文件夹(下面的目录结构)运行这些脚本

我猜你可能会对目录结构感兴趣,就在这里-

backend
|
├───flaskr
│   ├───templates
│   │   └───auth
│   └───__pycache__
|
|___flask-learn
这里是
\uuuu init\uuuuu.py
-

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:    
        os.makedirs(app.instance_path)
    except OSError:
        pass 

    from . import auth
    app.register_blueprint(auth.bp)

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, !'

    from . import db
    db.init_app(app)

    return app
import sqlite3

import click
from flask import current_app, g
from flask.cli import with_appcontext


def get_db():
    if 'db' not in g:
        g.db = sqlite3.connect(
            current_app.config['DATABASE'],
            detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory = sqlite3.Row

    return g.db


def close_db(e=None):
    db = g.pop('db', None)

    if db is not None:
        db.close()

def init_db():
    db = get_db()

    with current_app.open_resource('schema.sql') as f:
        db.executescript(f.read().decode('utf8'))


@click.command('init-db')
@with_appcontext
def init_db_command():
    """Clear the existing data and create new tables."""
    init_db()
    click.echo('Initialized the database.')

这里是
db.py
-

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:    
        os.makedirs(app.instance_path)
    except OSError:
        pass 

    from . import auth
    app.register_blueprint(auth.bp)

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, !'

    from . import db
    db.init_app(app)

    return app
import sqlite3

import click
from flask import current_app, g
from flask.cli import with_appcontext


def get_db():
    if 'db' not in g:
        g.db = sqlite3.connect(
            current_app.config['DATABASE'],
            detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory = sqlite3.Row

    return g.db


def close_db(e=None):
    db = g.pop('db', None)

    if db is not None:
        db.close()

def init_db():
    db = get_db()

    with current_app.open_resource('schema.sql') as f:
        db.executescript(f.read().decode('utf8'))


@click.command('init-db')
@with_appcontext
def init_db_command():
    """Clear the existing data and create new tables."""
    init_db()
    click.echo('Initialized the database.')


您的
db.py
缺少
init\u app
-功能:

def init_app(app):
    app.teardown_appcontext(close_db)
    app.cli.add_command(init_db_command)

嘿,非常感谢你指出这个愚蠢的错误:)