The flask object implements a WSGI application and acts as the central object. It is passed the name of the module or package of the application. Once it is created it will act as a central registry for the view functions, the URL rules, template configuration and much more.
The name of the package is used to resolve resources from inside the package or the folder the module is contained in depending on if the package parameter resolves to an actual python package (a folder with an __init__.py file inside) or a standard module (just a .py file).
For more information about resource loading, see
Usually you create a
Flask instance in your main module or
in the __init__.py file of your package like this:
from flask import Flask app = Flask(__name__)
About the First Parameter
The idea of the first parameter is to give Flask an idea what belongs to your application. This name is used to find resources on the file system, can be used by extensions to improve debugging information and a lot more.
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.
For example if your application is defined in yourapplication/app.py you should create it with one of the two versions below:
app = Flask('yourapplication') app = Flask(__name__.split('.'))
Why is that? The application will work even with __name__, thanks to how resources are looked up. However it will make debugging more painful. Certain extensions can make assumptions based on the import name of your application. For example the Flask-SQLAlchemy extension will look for the code in your application that triggered an SQL query in debug mode. If the import name is not properly set up, that debugging information is lost. (For example it would only pick up SQL queries in yourapplication.app and not yourapplication.views.frontend)
New in version 0.7: The static_url_path, static_folder, and template_folder parameters were added.
New in version 0.8: The instance_path and instance_relative_config parameters were added.
- param import_name
the name of the application package
- param static_url_path
can be used to specify a different path for the static files on the web. Defaults to the name of the static_folder folder.
- param static_folder
the folder with static files that should be served at static_url_path. Defaults to the
'static'folder in the root path of the application.
- param template_folder
the folder that contains the templates that should be used by the application. Defaults to
'templates'folder in the root path of the application.
- param instance_path
An alternative instance path for the application. By default the folder
'instance'next to the package or module is assumed to be the instance path.
- param instance_relative_config
if set to True relative filenames for loading the config are assumed to be relative to the instance path instead of the application root.