flask实例化

初始化:所有的Flask都必须创建程序实例,

web服务器使用wsgi协议,把客户端所有的请求都转发给这个程序实例

程序实例是Flask的对象,一般情况下用如下方法实例化

Flask类只有一个必须指定的参数,即程序主模块或者包的名字,__name__是系统变量,该变量指的是本py文件的文件名

 Flask 文档Flask 类的第一个参数称为 import_name。 它被描述为“应用程序包的名称”。 该文档建议你“通常”通过为此参数传递 __name__来创建 Flask 实例

Flask 接受作为 import_name 传递的参数,它是导入包的名称,并尝试使用它通过查找具有该名称的模块对象来确定应用程序的根路径。 一旦知道了这个路径,它就会拼接静态和模板目录名称,这就是它获取这些文件的地方。

__name__是一个python内建的系统变量。

 

  • 如果调用 Flask() 将包的名称作为参数传递,那么应用程序的根路径就是包所在的目录。
  • 如果调用 Flask() 将模块的名称作为参数传递,那么应用程序的根路径就是该模块所在的包的目录。
  • def get_root_path(import_name):
        """Returns the path to a package or cwd if that cannot be found.  This
        returns the path of a package or the folder that contains a module.
        Not to be confused with the package path returned by :func:`find_package`.
        """
        # Module already imported and has a file attribute.  Use that first.
        mod = sys.modules.get(import_name)
        if mod is not None and hasattr(mod, '__file__'):
            return os.path.dirname(os.path.abspath(mod.__file__))
    
        # Next attempt: check the loader.
        loader = pkgutil.get_loader(import_name)
    
        # Loader does not exist or we're referring to an unloaded main module
        # or a main module without path (interactive sessions), go with the
        # current working directory.
        if loader is None or import_name == '__main__':
            return os.getcwd()
    
        # For .egg, zipimporter does not have get_filename until Python 2.7.
        # Some other loaders might exhibit the same behavior.
        if hasattr(loader, 'get_filename'):
            filepath = loader.get_filename(import_name)
        else:
            # Fall back to imports.
            __import__(import_name)
            mod = sys.modules[import_name]
            filepath = getattr(mod, '__file__', None)
    
            # If we don't have a filepath it might be because we are a
            # namespace package.  In this case we pick the root path from the
            # first module that is contained in our package.
            if filepath is None:
                raise RuntimeError('No root path can be found for the provided '
                                'module "%s".  This can happen because the '
                                'module came from an import hook that does '
                                'not provide file name information or because '
                                'it\'s a namespace package.  In this case '
                                'the root path needs to be explicitly '
                                'provided.' % import_name)
    
        # filepath is import_name.py for a module, or __init__.py for a package.
        return os.path.dirname(os.path.abspath(filepath))

通过这个import_name确定Flask这个核心对象被创建的根目录,以获得静态文件和模板文件的目录 

  • 32
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值