DEV Community

Josué Rodríguez (He/Him)
Josué Rodríguez (He/Him)

Posted on

Flask folder structure and __init__.py file

Hello everybody!

Last week I started studying about how to develop a REST API with Flask. I know very little about Python, basically just syntax and how to run a script, but I'm completely blank when it comes to folder structure and special files (such as the __init__.py file). I have strong knowledge of JavaScript and TypeScript and about backends with Node (Express and Nest).

My first question is about the folder structure, I would like to know what is the best practice for structuring my project. Right now, my project is looking like this (trying to replicate how I do it with Node):

my_project/
-src/
--__init__.py
--blueprints/
---books/
----__init__.py
-tests/
--__init__.py
--test_books.py
-run.py
Enter fullscreen mode Exit fullscreen mode

(Your answers don't necessarily need to be for a Flask project, if you tell how you structure your random Python projects that would help too!)

My second question is about the __init__.py file. I've read that by adding that file you are making the folder a package or module. I had it inside my blueprints folder, but I removed it just to see what would happen and my project was unaffected.

So, my questions would be:

  • When should I add this file to my folders and when should I not?
  • When should I leave that file blank and when should I write code to it?

Thanks in advance for your time and your answers ✨

Top comments (0)