I learn from Larger Applications.In this document, It says: "all the view functions (the ones with a route() decorator on top) have to be imported in the init.py file. Not the object itself, but the module it is in."
I don't know why should when I do this: from . import views
,It succeed.Though from views import *
can also work well.
I organize these file like this:
myapplication/
runner.py
myflask/
__init__.py
views.py
templates/
static/
...
runner.py:
from testFlask import app
app.run()
myflask/__init__.py:
from flask import Flask
app = Flask(__name__)
from . import views # why this can work????
myflask/views.py:
from . import app
@app.route('/')
def index():
return 'Hello World!'
and I run it:
$ cd myapplication
$ python runner.py
It's OK to run this Flask app. However I want to know why from . import views
can solve this circle import problem in flask? And why the doc says: Not the object itself, but the module it is in????
However,when I do like this:
#some_dir/
# application.py
# views.py
#application.py
from flask import Flask
app = Flask(__name__)
import views # it doesn't work
# from views import * # it works
app.run()
#views.py
from application import app
@app.route('/')
def index():
return 'Hello World!'
#run it
$ python application.py
It doesn't work.