Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask('application') versus Flask(__name__)

In the official Quickstart, it's recommended to use __name__ when using a single module:

  1. ... If you are using a single module (as in this example), you should use __name__ because depending on if it’s started as application or imported as module the name will be different ('__main__' versus the actual import name). ...

However, in their API document, hardcoding is recommended when my application is a package:

So it’s important what you provide there. If you are using a single module, __name__ is always the correct value. If you however are using a package, it’s usually recommended to hardcode the name of your package there.

I can understand why it's better to hardcode the name of my package, but why not hardcoding the name of a single module? Or, in other words, what information can Flask get when it receives a __main__ as its first parameter? I can't see how this can make it easier for Flask to find the resources...

like image 961
nalzok Avatar asked Sep 08 '16 14:09

nalzok


People also ask

How do you name your Flask app?

In the first line we import the Flask class from the flask module. Then you create your application and pass the __name__ variable (pronounced dunder name) to the Flask class. The Flask route returns the value of __name__ so we can verify its value when we run the Flask application.

What is __ main __ In Flask?

The condition __name__ == "__main__" ensures that the run() method is called only when main.py is run as the main program. The run() method will not be called if you import main.py in another Python module. Note: The development server comes with Flask is only used for development purpose.

What is Flask app?

app is an instance of Flask , taking in the __name__ of the script file. This lets Python know how to import from files relative to this one. The app. route decorator decorates the first view function; it can specify one of the routes used to access the application.

What is the class to which an instance of a Flask application belongs to?

A Flask application is an instance of the Flask class. Everything about the application, such as configuration and URLs, will be registered with this class.


1 Answers

__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.

like image 172
davidism Avatar answered Sep 27 '22 00:09

davidism