Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

flask

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!

like image 543
linksta7 Avatar asked Dec 17 '25 08:12

linksta7


1 Answers

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.

like image 119
Prashant Kumar Avatar answered Dec 20 '25 00:12

Prashant Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!