Accessing Configurations and Settings in Python

The Problem

When coding in Python, we need a way to store configurations and
settings in a file, then be able to access them in the code. Our
requirements for accessing these settings are:

  • Simple to learn and use — We would like to target novice coders
  • Easy to understand
  • Hierarchical of settings

References

The Options

Currently, there are a few options for accessing configurations files,
some of them are:

  • INI file format
    • ConfigParser module
    • ConfigObj
    • INITools
    • cfgparse
  • Straight Python code, via the import mechanism
  • YAML file format with yaml module
  • HierConfig
  • JSON
  • Vinay Sajip’s config module
  • tconfpy

In the following sections, we are going to discuss the advantages and
disadvantages of some of these.

ConfigParser

Advantages

  • Library comes with Python installation, no need to install it
  • Ability to specify a default section
  • Low learning curve

Disadvantages

  • Not zero learning-curve as with straight Python code method
  • No hierarchical of data: the default data layout is just one layer
    deep: A configuration file consists of one or more sections and each
    section contains one or more settings.

ConfigObj

Advantages

  • Simple to learn: the configuration is a two-dimensional array: one for
    each section, and one for the settings
  • Round-trip: read/write from/to files
  • Many other features

Disadvantages

  • Need installation

INITools

This tool is abandoned, so we only mention here for completeness.

cfgparse

Advantages

  • Round-trip read and write
  • Many features
  • Ability to specify a default section

Disadvantages

  • Need installation
  • Not simple to use
  • Higher learning curve than other INI solutions
  • INI file can without section header, which is non-standard

Straight Python Code

Advantages

  • Natively supported: We don’t need any module to parse
  • Low learning curve: The users do not need to learn another
    configuration file format
  • Versatile: This file format supports all kind of data types: integer,
    string, dictionary, array, …
  • Simple usage: The users only need to make sure that the settings file
    is in the same directory as the script

Disadvantages

  • No “default” mechanism as in INI
  • Cannot merge different settings files into one
  • No simple mechanism for dynamically load configuration files. For
    example, the ability to specify different configuration files from the
    command line

Conclusion

Of these solutions, we prefer the the native, straight Python code for
its simplicity, ease of use, and zero learning curve. In the next posts,
I will discuss the straight python code solution in details.

2 thoughts on “Accessing Configurations and Settings in Python

  1. Brendan Miller

    I tend to like the straight python code approach as well, as it’s a lot like an rc file. As far as merging python modules go, you can do something like this:

    defaults = {x: 1}

    import my_config
    values = dict(defaults.items() + [x for x in my_config.__dict__])

    values.x # this will have default value 1, but will be overridden by an x in my_config

    If you want to dynamically import a module based on user data, you can do this:
    my_module = __import__(“mymodulename”)

    This seems to work similar to require in nodejs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s