2

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 :)

LimitIt
  • 31
  • 1
  • 4
  • 1
    Do you have an `app` variable in an `app.py` file that you are trying to import via `from app import app`? Also call stacks are useful when we can see the full traceback. The error alone is almost useless. – Error - Syntactical Remorse Jul 23 '19 at 19:34
  • I have an app in the __init__.py, updated so you can see structure. Will try to get the traceback but all I am getting is what I posted, will see if i can figure out how to get more -- edit: it is dunder init dunder .py that i have but the markdown on stackoverflow is making it bold – LimitIt Jul 23 '19 at 19:45
  • Please edit your post to add the __full__ traceback. – bruno desthuilliers Jul 24 '19 at 07:47

2 Answers2

3

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/.

1

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.

mathuias
  • 11
  • 2