flask的Flask类初始化参数中有一个instance_path
,默认为None
。
在官网上看了相关文档Instance Folders,反复看了几遍还是不太明白其意义何在,看flask相关案例也没有见过使用的。
自己在机器上做了下测试,输出如下:
view函数加一句print current_app.root_path+'\n'+current_app.instance_path
,可以得到:
/home/xxx/Documents/flasky/flasky/app
/home/xxx/Documents/flasky/flasky/instance
其跟root_path的区别仅在于最后一个地方由app
变成了instance
,这参数是0.8版本才加的,意义何在(既然它和root_path基本一样),加了它之后能够实现什么之前所不能实现的东西吗?
After reading the document, there is this paragraph, I wonder if it can answer the questioner’s doubts:
The instance folder is designed to not be under version control and be deployment specific. It’s the perfect place to drop things that either change at runtime or configuration files.
My understanding is that similar to the separation of front-end html and css, the instance folder is separated from the default folder to reduce coupling. It is convenient to modify the instance later without affecting the folder. The None mentioned by the question means that the default instance is under the root folder, and the example corresponding to the question is under the flasky directory.
Now the new version can specify a specific location by setting the instance_path parameter, for example, at the same level as the flasky directory.