Python Flask Tutorial: Full-Featured Web App Part 5 - Package Structure

Corey Schafer · 361K views · 2018 · 20:38

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering import errors and project layout questions in Flask apps, especially circular imports and package structure problems.

Recommended by 1 people across 1 different months, 2019 to 2021.

What it is

Part 5 of Corey Schafer's Flask tutorial series shows how to restructure a single-file Flask app into a Python package with an __init__.py and separate modules for routes, models and forms. It focuses on how this layout makes imports across the application cleaner and avoids circular import problems.

What people said

Recommends Aven Desta · 2021-02 · Stack Overflow · on “receiving ImportError: cannot import name x from y”

You are making a circular import. In __init__.py , you're importing refactor and circularly in refactor.py you're using __init__ Here is how python deals with your code. It first looks at refactor.py and tries to import __init__ . But to import __init__ , python has to run the file __init__.py . But again __init__.py requires refactor , and has to go back to refactor.py . At this point python stops the loop and raises Import Error . Python import breaks if your import is circular. Read here [LINK] for more. If you're working with Flask framework, here is a [this video] tutorial to help you structure your modules to avoid circular import

Cites Dominic DiTaranto · 2019-02 · Stack Overflow · on “Python: Indexing error only in flask routes.py, not anywhere else”

Okay so there were a few problems that were causing this issue. My project layout was incorrect. If you are having this problem check these two links: [LINK] [this video] I made the path for os.walk() start in the parent directory of what was written prior 2.5 this may cause issues if you try to os.path.join the path used in os.walk() and the filename (result of os.walk()) when you try to add these paths to html because you will be one directory higher than you need to for html (sorry if this is confusing to you, it is to me as well). For some reason, os.walk() wants to see one more directory than html, so instead of recycling the path from os.walk(path) you should do this: menu_image_list = [] f

Cites gagandeep · 2020-04 · Stack Overflow · on “ModuleNotFoundError: Flask”

I am calling a module inside urbancloth package from home.py This runs the __init__ file but doesn't run another module present in urbancloth. The directory structure is as follows - [LINK] The code in home.py : from urbancloth import app if __name__ == '__main__': app.run(debug =True) The code in __init__.py : from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SECRET_KEY'] = 'b0fe7021532f0541f87226aafd71ec77' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' db = SQLAlchemy(app) from urbancloth import routes and the error message is- [LINK] I am following Sir CoreySchafers Flask tutorials [this video] Github for original code [LINK]