Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot initialize flask initdb (Flask Tutorial Step4)

Tutorial link: http://flask.pocoo.org/docs/0.11/tutorial/dbinit/#tutorial-dbinit

I am following the Flask tutorial. This is the current setup of my python script. At the end of the tutorial, I am trying to initialize the database. But for some reason, I kept on getting the same error.

# all the imports
import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash

# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)

# Load default config and override config from an environment variable
app.config.update(dict(
    DATABASE=os.path.join(app.root_path, 'flaskr.db'),
    SECRET_KEY='development key',
    USERNAME='admin',
    PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)

def connect_db():
    """Connects to the specific database."""
    rv = sqlite3.connect(app.config['DATABASE'])
    rv.row_factory = sqlite3.Row
    return rv

def init_db():
    db = get_db()
    with app.open_resource('schema.sql', mode='r') as f:
        db.cursor().executescript(f.read())
    db.commit()

@app.cli.command('initdb')
def initdb_command():
    """Initializes the database."""
    init_db()
    print 'Initialized the database.'


def get_db():
    """Opens a new database connection if there is none yet for the
    current application context.
    """
    if not hasattr(g, 'sqlite_db'):
        g.sqlite_db = connect_db()
    return g.sqlite_db

@app.teardown_appcontext
def close_db(error):
    """Closes the database again at the end of the request."""
    if hasattr(g, 'sqlite_db'):
        g.sqlite_db.close()

This is the input of my command:

flask initdb

This is the output:

Usage: flask [OPTIONS] COMMAND [ARGS]...

Error: No such command "initdb"
like image 630
YAL Avatar asked Nov 19 '16 18:11

YAL


4 Answers

I think you should follow this:

  1. edit the configuration in the flaskr.py file or export an FLASKR_SETTINGS environment variable pointing to a configuration file.

  2. install the app from the root of the project directory

    pip install --editable .
    
  3. Instruct flask to use the right application

    export FLASK_APP=flaskr
    
  4. initialize the database with this command:

    flask initdb
    
  5. now you can run flaskr:

    flask run
    

Pay attention to install --editable correctly. I don't see the "." the first time.

like image 138
vuongbui Avatar answered Oct 09 '22 16:10

vuongbui


the correct expression should be:

export FLASK_APP=flaskr.py

Note: no spaces around =.

Following screenshot show you outputs with different envvar FLASK_APP :

screenshot

like image 44
pumpkindle Avatar answered Oct 09 '22 16:10

pumpkindle


Ran into the same issue, fixed it with

python3 -m flask initdb

I had to do everything for the tutorial with python -m flask <command>

I'm guessing its something to do with python3 instead of python2 but I'm new to python so not super sure.

like image 5
Sam Avatar answered Oct 09 '22 17:10

Sam


In my case, following works:

export FLASK_APP=flaskr.flaskr

flask initdb

like image 2
Larry Avatar answered Oct 09 '22 17:10

Larry