How does python import order affect names? -
i'm doing flask tutorial (http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world) , came upon behaviour couldn't explain. main directory structure of tutorial :
microblog | |---- app | |---- __init__.py | |---- views.py | |---- flask |---- run.py
and contents of files :
microblog/run.py
#!flask/bin/python app import app app.run(debug=true)
microblog/app/init.py
from flask import flask app = flask(__name__) app import views
microblog/app/views.py
from app import app @app.route("/") @app.route("/index") def index(): return "hello world!"
everything works if transpose these 2 lines:
app = flask(__name__) app import views
in views.py , execute run.py get:
importerror: cannot import name app
why happen?
because you're trying import newly created variable app
. if want import variable modules, use importlib
package:
my_module = importlib.import_module(app, 'view')
Comments
Post a Comment