__name__
is just a convenient way to get the import name of the place the app is defined. Flask uses the import name to know where to look up resources, templates, static files, instance folder, etc. When using a package, if you define your app in __init__.py
then the __name__
will still point at the "correct" place relative to where the resources are. However, if you define it elsewhere, such as mypackage/app.py
, then using __name__
would tell Flask to look for resources relative to mypackage.app
instead of mypackage
.
Using __name__
isn't orthogonal to "hardcoding", it's just a shortcut to using the name of the package. And there's also no reason to say that the name should be the base package, it's entirely up to your project structure.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…