Hacker News new | ask | show | jobs
by etiennebch 2249 days ago
I have been using Flask for a while now (to code rest apis). The circular import issue can be a pain. I blame most tutorials and examples out there for this. As the article mentions, putting code in init.py can lead to issues because the file is read when importing modules in the directory it is declared. Better to keep it empty. Also tutorials rarely mention how to use the factory pattern effectively which prevents most of the issue. This is how flask extensions are implemented by the way, with init_app function doing the initialization. Often instead, you see app = Flask() declared at the top of the init.py Not to mention using the app.url_rule decorator on views (which leads to importing the global object) instead of using blueprints to register views and registering blueprints in the app factory