Flask的__name__的作用

Posted 哦...

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask的__name__的作用相关的知识,希望对你有一定的参考价值。

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 :file:`__init__.py` file inside) or a standard module (just a ``.py`` file).

Usually you create a :class:`Flask` instance in your main module or in the :file:`__init__.py` file of your package like this:

from flask import Flask
app = Flask(__name__)

The idea of the first parameter is to give Flask an idea of what belongs to your application.  This name is used to find resources on the filesystem, 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.

以上内容粘贴自2.2.2版本Flask源码的说明部分(有部分删改)

以上是关于Flask的__name__的作用的主要内容,如果未能解决你的问题,请参考以下文章

简单的Flask项目,hello world!

Flask基础之一

flask第一级

Flask的路由,视图和相关配置

Flask('application') 与 Flask(__name__)

Flask 蓝图