1
To doc of Flask, despite using in its examples, warns at the end of the page about the bad practice of using circular imports.
Another thing that bothers me is creating "global objects" inside a file __init__.py
.
What would be the other solution?
Filing cabinet __init__.py
of the main module, that is, the main folder of the application:
from flask import Flask
app = Flask(__name__)
import yourapplication.views
The views.py file (The application view):
from yourapplication import app
@app.route('/')
def index():
return 'Hello World!'
The structure of the project:
/yourapplication
setup.py
/yourapplication
__init__.py
views.py
/static
style.css
/templates
layout.html
index.html
login.html
...
Credits from the sample code: http://flask.pocoo.org/docs/1.0/patterns/packages/#simple-Packages
And why the object
app
accurate be defined in__init__.py
?– Woss
Also wanted to know. I’m starting in the development of Flask. Looking fast it seems to me it’s by the fact that the
__init__.py
is called at the time the module is imported then the object would be created.– Matheus Saraiva