Home > Blockchain >  Flask tutorial. FileNotFoundError
Flask tutorial. FileNotFoundError

Time:04-04

I'm a beginner in Python and flask. I am going through the Flask tutorial up to Define and Access the Database section.

wrote up all the codes, saved, and execute the command below to initalise the DB. flask init-db

However, I get the error on the terminal as flows

FileNotFoundError: [Errno 2] No such file or directory: /Desktop/flask-tutorial/instance/schema.sql'

I double-checked the codes to find out what was wrong, searched through StackOverflow,w and find some similar issues but they end up not working for me.

--Addition--

__init__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

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, world! this is my first flask app'

    from . import db
    db.init_app(app)
    
    return app

db.py

import sqlite3

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

def init_db():
    db = get_db()

    with current_app.open_instance_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.')
    

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_app(app):
    app.teardown_appcontext(close_db)
    app.cli.add_command(init_db_command)

Tree

.
├── flaskr
│   ├── db.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── db.cpython-39.pyc
│   │   └── __init__.cpython-39.pyc
│   └── schema.sql
├── instance
│   └── flaskr.sqlite

CodePudding user response:

I think you have downloaded the file in virtual env and trying to access it outside your env.When you create virtual env,files are downloaded inside that env irrespective of that whole system.So you have to first enter in your env and then run the code.

CodePudding user response:

Your code has open_instance_resource, which is looking for instance/schema.sql - but your schema.sql is not there. The original code has open_resource, which looks relative to root_path.

  • Related