What's the official way of storing settings for python programs?

后端 未结 13 1220
傲寒
傲寒 2020-12-12 23:58

Django uses real Python files for settings, Trac uses a .ini file, and some other pieces of software uses XML files to hold this information.

Are one of these approa

相关标签:
13条回答
  • 2020-12-13 00:22

    I use a shelf ( http://docs.python.org/library/shelve.html ):

    shelf = shelve.open(filename)
    shelf["users"] = ["David", "Abraham"]
    shelf.sync() # Save
    
    0 讨论(0)
  • 2020-12-13 00:23

    As many have said, there is no "offical" way. There are, however, many choices. There was a talk at PyCon this year about many of the available options.

    0 讨论(0)
  • 2020-12-13 00:27

    Just one more option, PyQt. Qt has a platform independent way of storing settings with the QSettings class. Underneath the hood, on windows it uses the registry and in linux it stores the settings in a hidden conf file. QSettings works very well and is pretty seemless.

    0 讨论(0)
  • 2020-12-13 00:28

    I am not sure that there is an 'official' way (it is not mentioned in the Zen of Python :) )- I tend to use the Config Parser module myself and I think that you will find that pretty common. I prefer that over the python file approach because you can write back to it and dynamically reload if you want.

    0 讨论(0)
  • 2020-12-13 00:29

    Don't know if this can be considered "official", but it is in standard library: 14.2. ConfigParser — Configuration file parser.

    This is, obviously, not an universal solution, though. Just use whatever feels most appropriate to the task, without any necessary complexity (and — especially — Turing-completeness! Think about automatic or GUI configurators).

    0 讨论(0)
  • 2020-12-13 00:29

    It depends largely on how complicated your configuration is. If you're doing a simple key-value mapping and you want the capability to edit the settings with a text editor, I think ConfigParser is the way to go.

    If your settings are complicated and include lists and nested data structures, I'd use XML or JSON and create a configuration editor.

    For really complicated things where the end user isn't expected to change the settings much, or is more trusted, just create a set of Python classes and evaluate a Python script to get the configuration.

    0 讨论(0)
提交回复
热议问题