什么是存储Python程序设置的官方方式?

Django使用真正的Python文件进行设置,Trac使用.ini文件,其他一些软件使用XML文件来保存这些信息。

Guido和/或Python社区中的其中一种方法是否比其他方法更有福?

取决于主要的目标受众。

如果是修改文件的程序员,只需使用像settings.py这样的python文件即可

如果是最终用户,那么考虑一下ini文件。

正如很多人所说,没有“官方”的方式。 但是,有很多select。 今年在PyCon上有关于许多可用选项的讨论。

我使用一个架子( http://docs.python.org/library/shelve.html ):

 shelf = shelve.open(filename) shelf["users"] = ["David", "Abraham"] shelf.sync() # Save 

不知道这是否可以被认为是“官方”的,而是在标准库中: 14.2。 ConfigParser – configuration文件parsing器 。

显然,这不是一个通用的解决scheme。 只要使用最合适的任务,没有任何必要的复杂性(尤其是 – 图灵完备性!想想自动或GUIconfiguration器)。

还有一个选项,PyQt。 Qt具有与QSettings类一起存储设置的平台独立方式。 在引擎盖下,在Windows上使用registry,在Linux中它将设置存储在一个隐藏的conf文件中。 QSettings工作得很好,而且很好看。

我不确定是否有“官方”的方式(Python中没有提到:)) – 我倾向于使用Configparsing器模块,我认为你会发现这很常见。 我比较喜欢python文件的方式,因为你可以写回来,如果你想要dynamic的重新加载。

据我所知,没有幸运的解决办法。 没有正确或错误的方式来存储应用程序设置,XML,JSON或所有types的文件都可以,只要你是舒适的。 对于python,我个人使用pypref非常简单,跨平台和直接。

pypref是非常有用的,因为可以存储静态和dynamic设置和偏好…

  from pypref import Preferences # create singleton preferences instance pref = Preferences(filename="preferences_test.py") # create preferences dict pdict = {'preference 1': 1, 12345: 'I am a number'} # set preferences. This would automatically create preferences_test.py # in your home directory. Go and check it. pref.set_preferences(pdict) # lets update the preferences. This would automatically update # preferences_test.py file, you can verify that. pref.update_preferences({'preference 1': 2}) # lets get some preferences. This would return the value of the preference if # it is defined or default value if it is not. print pref.get('preference 1') # In some cases we must use raw strings. This is most likely needed when # working with paths in a windows systems or when a preference includes # especial characters. That's how to do it ... pref.update_preferences({'my path': " r'C:\Users\Me\Desktop' "}) # Sometimes preferences to change dynamically or to be evaluated real time. # This also can be done by using dynamic property. In this example password # generator preference is set using uuid module. dynamic dictionary # must include all modules name that must be imported upon evaluating # a dynamic preference pre = {'password generator': "str(uuid.uuid1())"} dyn = {'password generator': ['uuid',]} pref.update_preferences(preferences=pre, dynamic=dyn) # lets pull 'password generator' preferences twice and notice how # passwords are different at every pull print pref.get('password generator') print pref.get('password generator') # those preferences can be accessed later. Let's simulate that by creating # another preferences instances which will automatically detect the # existance of a preferences file and connect to it newPref = Preferences(filename="preferences_test.py") # let's print 'my path' preference print newPref.get('my path') 

这在很大程度上取决于你的configuration有多复杂。 如果你正在做一个简单的键值映射,并希望能够使用文本编辑器来编辑设置,我认为ConfigParser是一个很好的select。

如果你的设置很复杂,包括列表和嵌套的数据结构,我会使用XML或JSON,并创build一个configuration编辑器。

对于最终用户不希望更改设置或更可信的复杂事情,只需创build一组Python类并评估一个Python脚本即可获得configuration。

对于Web应用程序,我喜欢使用OS环境variables: os.environ.get('CONFIG_OPTION')

这适用于部署之间不同的设置。 你可以在这里阅读更多关于使用envvariables的基本原理: http : //www.12factor.net/config

当然,这只适用于只读值,因为对环境的更改通常不会持久。 但是如果你不需要写入权限,他们是一个非常好的解决scheme。

为什么Guido会祝福那些超出他范围的东西呢? 没有什么特别的祝福。

最简单的方法之一就是使用json模块。 将文件保存在config.json ,具体细节如下所示。

将数据保存在json文件中:

 { "john" : { "number" : "948075049" , "password":"thisisit" } } 

从json文件读取:

 import json #open the config.json file with open('config.json') as f: mydata = json.load(f) ; #Now mydata is a python dictionary print("username is " , mydata.get('john').get('number') , " password is " , mydata.get('john').get('password')) ; 

这是更方便。 没有官方的说法。 但是使用XML文件是有意义的,因为它们可以被各种其他应用程序/库所操纵。