(My Notes) Configuration Files in Python

Victor Fernandez
4 min readMay 12, 2022
Photo by Sigmund on Unsplash

The Problem:

When coding, I will have different lines of code holding the configuration of a test environment and production environment or lines with a sandbox endpoint of an API and others with the real endpoint.

I need to comment and uncomment these lines depending on the intention in mind when running the script.

The Solution:

Create a data structure that holds the different configurations for different intentions for running the code; coding, testing, or deploying it.

The Implementation:

Use the configparser module and the ConfigParser class to create a configuration file that will hold the configuration items for the different intentions.

Example: A file with two sets of instructions. One set of instructions uses when debugging. It will contain debugging messages and sandbox endpoints for the API calls. The second set of instructions for deployment will have the correct endpoints and will log just errors if they occur.

  1. Quick Overview (Basic configuration file).
  2. How to make the configuration file.
  3. How to read the configuration file.
  4. How to update the configuration file.

1. Quick Overview (Basic Configuration File)

  • The Files consist of sections.
  • The name of the section is within [] , Example: [DEFAULT] or [testing].
  • Each section will contain keys with values
  • The extension of the file is .ini
A configuration file — Content from python docs

Supported structure

  • Section names are case sensitive [Default] is different than [default].
  • Spaces are allowed in the keys and the values, my key = my value
  • = or : can be used to separate keys and values.
  • keys with no values can omit = or it can be used if is not followed by any character, my key without value or my key without value = are correct.
  • Comments can be added with # or ; like # I am a comment and ; me too.
  • A section can be indented.
Example of a configuration file

2. How to make the configuration file

I can make a config file in two ways:

  1. Creating the file and saving it with a .ini extension.
  2. Creating the file programmatically.

Creating a file programmatically can be done using a code similar to this:

Creating a configuration file programmatically
Configuration file

🔥 Section names are case-sensitive but keys are not.

All the values stored in the keys are strings.

I can use getboolean() to get a boolena value for keys holding strings like yes, no 'on', 'off', 'true', 'false', '1', '0'.

3. How to read the configuration file.

ConfigParser class provides methods to interact with the configuration file.

read()

It allows me to read the .ini file.

sections()

It returns the section names present in the config file.

getboolean()

It will return a Boolean value depending on the value stored in the key, values must be 'yes'/'no', 'on'/'off', 'true'/'false' and '1'/'0'.

Here are some examples of interactions with a config file.

Reading a configuration file.

Like the dictionaries, I can provide a Fallback value, so when the key doesn’t exist, I can create it and give it a value.

>>> default.get('last_name','Fernandez')
Fernandez

4. How to update the configuration file.

To update it I can use the following methods:

  • add_section() to add a new section.
  • set(’my_section’,’my_new_key’,’my_new_value’)To add a new key-value pair to a specific session.
updating configuration file

I need to save the changes by writing the configuration file one more time.

Final Thoughts

  1. Configuration files can be a valuable tool to keep the information I don’t want to commit to a repository.
  2. Configuration files can hold the logging configuration like the level of logging and the string format.
  3. Database URI and SQL queries can be stored in a configuration file.

Victor Fernandez

--

--

Victor Fernandez

I’m Victor, I’m a Field Application Engineer for a CCTV manufacturer. I love Raspberry Pi, Python, and Microcontrollers and I write about my personal projects.