How does python import order affect names? How does python import order affect names? flask flask

How does python import order affect names?


Because you're trying to import from the newly created variable app. If you want to import variable modules, then use importlib package:

my_module = importlib.import_module(app, 'view')


Contrary to what the other answer says, this is a circular import problem. app.__init__ tries to import app.views, which tries to import the app.app Flask created in app.__init__. If the Flask is created before app.__init__ imports app.views, app.views finds app.app. If the Flask is created after the import, it isn't there yet when app.views tries to find it.

Circular imports cause all sorts of horrible problems. It might be difficult, but the best way to handle them is generally to reorganize your code so there aren't circular imports.