Answer the question
In order to leave comments, you need to log in
How to properly include imports in Flask?
There is the following project structure:
from app.REST import mod_books
app = Flask(__name__)
db = SQLAlchemy(app)
app.register_blueprint(mod_books, url_prefix='/books')
mod_books = Blueprint("books", __name__)
books_api = restful.Api(mod_books)
from app.REST import books
from app.REST import books_api
from app import db
from app.items import Book, Author
...
class BooksListApi(Resource):
...
books_api.add_resource(BooksListApi, '/')
...
Answer the question
In order to leave comments, you need to log in
I just ran into this problem yesterday.
It is enough to write in one place not from A import B
, but from A import *
, for example, in REST/books.py:from app import *
Look towards these repositories:
https://github.com/sloria/cookiecutter-flask
https://github.com/imwilsonxu/fbone
They have a more complex project structure, but also more convenient. Logically separated: creating an app, creating extensions, registering Blueprints and extensions, and more. Look in the "app.py" files. At first it may seem like an overhead, but believe me, it is almost certain that your project will grow, new parts will be added, but this structure will remain unchanged, and the project will look harmonious and understandable.
The first repository is especially good because it is a cookiecutter template ( https://github.com/audreyr/cookiecutter).If you have not come across this application, then be sure to try it, as it can greatly simplify the creation of the initial structure of projects.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question