LimitIt
LimitIt

Reputation: 41

flask db init Error: Could not import "app.run"

I am trying to initialise my db, and everytime i run the flask db init command, I get that error.

I am unsure why, my FLASK_APP and FLASK_ENV are set correctly.

I have been reorganising the project structure and file names to abide more with the flask guidelines in the moments before this.

run.py

from app import app

app.run(debug=True)

config.py

import os


basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_TRACK_MODIFICATIONS = True

init.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate


app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

migrate = Migrate(app, db)

from app import views, models

db_create.py

from config import SQLALCHEMY_DATABASE_URI
from app import db
import os.path


db.create_all()

models.py

from app import db


class Property(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    address = db.Column(db.String(500), index=True, unique=True)
    start_date = db.Column(db.DateTime)
    duration = db.Column(db.Integer)
    rent = db.Column(db.Float)

views.py

from app import app
from flask import render_template

@app.route('/')
def index():
    return render_template('index.html')


Error:

$ flask db init Usage: flask db init [OPTIONS]

Error: Could not import "app.run".

edit: Folder structure so you can understand better:

config.py
run.py
app/
    __init__.py
    db_create.py
    models.py
    views.py
    static/
    templates/
        index.html
        layout.html

adding again: The issue is not running my code, it is when I am trying to initialise my db using flask db init

I get the error in my terminal when trying to run this, hoping someone can help me figure out why and then fix it, thanks for reading :)

Upvotes: 3

Views: 9955

Answers (2)

mathuias
mathuias

Reputation: 11

you need to check the flask app variable again with echo $FLASK_APP.
In Miguel's Tutorial, FLASK_APP is set in .flaskenv, however you need to make sure that FLASK_APP is really set to your starter file. I had the default value set to run.py, which impaired starting up the migration repo. so set it with export FLASK_APP=<your-app-starter-file>.py.

Upvotes: 1

Kishore Kumar Singh
Kishore Kumar Singh

Reputation: 126

Please make sure that your working directory is not inside your flask app app/.

    flask_app/
        config.py
        run.py
        app/
            __init__.py
            db_create.py
            models.py
            views.py
            static/
            templates/
                index.html
                layout.html

In this case, your working directory should be flask_app/.

Upvotes: 3

Related Questions