flask中能够配置一个字符串导入settings下的配置文件python
app.config.from_object("settings.ProductionConfig")
这里就是来说解一下这个究竟是怎么实现的。web
例:
这是just_xxx.py里面的内容django
# -*- coding: utf-8 -*- # @Time : 2019/6/17 上午 11:50 # @Author : lh # @Email : 22@qq.com # @File : just_xxx.py # @Software: PyCharm class MyValue(object): VALUE1 = 'this is a vaule1' VALUE2 = 'this is a vaule2'
test_use.pyflask
# -*- coding: utf-8 -*- # @Time : 2019/6/17 上午 11:52 # @Author : lh # @Email : 2470937072@qq.com # @File : test_use.py # @Software: PyCharm import importlib my_path = 'test1.just_xxx.MyValue' path, name = my_path.rsplit('.', maxsplit=1) # 进行反向切片。 a = importlib.import_module(path) # 获取<module 'test1.just_xxx' from 'I:\\flask_about\\flask_test1\\test1\\just_xxx.py'> cls = getattr(a, name) # 使用反射 for key in dir(cls): # 遍历内容 if key.isupper(): # 筛选大写的属性 print(key, getattr(cls, key))
这是个人项目目录
app
这就是flask和django里面的配置文件的实现原理了。svg