1

Following this tutorial on how to structure a Flask app, I have:

project/
       __init__.py
       app.py
       models/
             __init__.py
             base.py

base.py

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

models/__init__.py

from .base import db

def init_app(app):
    db.init_app(app)

project/__init__.py

from flask import Flask

def create_app()
    from . import models, routes, services
    app = Flask(__name__)
    models.init_app(app)
    # routes.init_app(app)
    # services.init_app(app)
    return app

finally, in app.py, I try to run it:

from . import create_app

app = create_app()

if __name__ == '__main__':
    app.run(use_reloader=True, threaded=True, debug=True)

but I'm getting the error:

    from . import create_app
ValueError: Attempted relative import in non-package

Am I building it right, what am I doing wrong?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
  • 1
    Possible duplicate of [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Fine Dec 28 '18 at 08:13

1 Answers1

2

I guess you are running your program by:

python project/app.py

In this case, you are not treat your "project" as a python package, which will raise the error you got. Instead, you can run your project with:

FLASK_APP=project.app flask run
lepture
  • 2,307
  • 16
  • 18