Flask: What is the use of __init__.py vs run.py? And are blueprints standard things to use in most Flask web applications?

Viewed 7680

I recently started getting into Flask web dev and I've probably gone through 5 different courses/tutorials now, and they all seem to have different ways/conventions of doing things. I was hoping I could receive some clarity here!

  1. Is __init__.py mandatory? I see some tutorials use them and some don't. From my understanding, simpler projects don't have a need for __init__.py due to their simplicity and lack of modules.
  2. Do I need a run.py? Again, I seem some tutorials use them and some don't so I'm confused on this one. From my knowledge, it seems like run.py is used to initialize the app while __init__.py is used to initialize packages (folders)? If so why do some people only use __init__.py?
  3. Are blueprints a standard thing used in nearly all flask-based web apps? I ask because some courses seem to teach them as basic concepts, while others simply don't use them. What's the difference between using blueprints and simply just "importing" the module? Why do some projects use them while others don't?
  4. Should the following block exist in essentially every module to ensure that it's not the main module being run?

​

if __name__ == '__main__':
    app.run(debug=True)
  1. Following up on the blueprint stuff, I have a project I use as a flask playground in order for me to mess around in a useless flask project for me to mess around without worrying I'm messing up a real project. I'm currently importing a "users.py" file for me to try out SQL Alchemy. I'm following a tutorial on SQL Alchemy where they do not use blueprints, thus simple do the standard app=Flask(__name__) thing. However, I'm using blueprints and have this sliver of code:

​

# Initialize as blueprint
users = Blueprint('users', __name__, url_prefix="/users")
# Load .env.local file and specify relative path
load_dotenv(dotenv_path="./.env.local") 
# Database config
users.config["SECRET_KEY"] = "mysecretkey"
users.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLITE_URI", "")
users.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

The tutorial uses "app.config" obviously, but my "app" exists in my __init__.py and not in this users.py , so is there a way of using config in this users file? Should it be done in __init__.py instead?

Sorry for the lengthy post of questions, thank you in advance for anyone who can help me!

3 Answers

I can answer a few questions from your list that I understood.

Question1

init.py is not a mandatory file. Files named __init__.py are used to mark directories on disk as Python package directories. So it really depends on your structure of projects. Ideally if you have a project with multiple folders, you might have to make them as python package directories.

Here is an example. If you have the files

project/my_app/__init__.py
project/my_app/module.py

you can import the code in module.py as

from my_app import module

If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.

Question 2

Ideally yes, run.py is used to initialize or start the server application. You may also see the file named app.py being used instead at some places.

If you are creating a flask application and want your api's to be exposed for use, you will have to start or run your flask app.

app.run(debug=True) does this.

Regarding difference between app.py and __init__.py :

  • run.py --> If you want to start your application, you will have to explicitly execute run.py file.
  • '__init__.py --> The code in init.py is run whenever you import anything from the package. That includes importing other modules in that package.

Question 4

Simple answer is NO.

The statement if __name__ == '__main__': is used only if you expect the python file to be executed using its file name.

If you have a python file that only has helper functions and does not do anything else, you may not include this statement.

Related