python - Why `from . import views` can solve circle import in Flask? -


this question has answer here:

i learn larger applications.in document, says: "all view functions (the ones route() decorator on top) have imported in init.py file. not object itself, module in."
don't know why should when this: from . import views,it succeed.though from views import * can work well.
organize these file 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__) . import views # why can work???? 

myflask/views.py:

from . import app @app.route('/') def index():     return 'hello world!' 

and run it:

$ cd myapplication $ python runner.py 

it's ok run flask app. want know why from . import views can solve circle import problem in flask? , why doc says: not object itself, module in????

however,when this:

#some_dir/ #  application.py #  views.py  #application.py flask import flask app = flask(__name__) import views # doesn't work # views import * # works app.run()  #views.py application import app @app.route('/') def index():     return 'hello world!'  #run $ python application.py 

it doesn't work.

it circular import. in case, variable might have been problematic (app) has been defined in imported script, import causes first "app" instance overwritten imported "app" instance. has no practical effect.

for details circular import situation, please read post.

if want follow pattern large flask application, should blueprints , application factories.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -