Table of Contents
Motivation
Sometimes you need a configuration for your project to adapt to different environments.
The easiest way is to alter some variables / constants in your program code.
But Hold your horses. This isn’t always a good idea. What if you package your code into an executable with e.g. PyInstaller?
Maybe the user of your tool needs to be able top do some configuration as well.
INI files
The INI file goes way back to MS-DOS. INI is short for initialization and offers text-based key-value pairs for properties and sections for organizing properties.
Minimum Runnable Solution
ini file
Create a textfile e.g. backup_config.ini with the following content
[BACKUP]
ServerAddress = smb://192.168.0.101
[BACKUP] is the name of the section
ServerAddress is a property
configparser module
import configparser
def test_configparser():
config = configparser.ConfigParser()
config.read('./backup_config.ini')
assert config.sections() == ['BACKUP']
assert config['BACKUP']['ServerAddress'] == "smb://192.168.0.101"
Reading Bolean Values
[PLATFORMS]
Creatronix: no
YouTube: yes
def test_configparser_boolean():
config = configparser.ConfigParser()
config.read('./metadata.ini')
assert config.getboolean("PLATFORMS", "YouTube") is True
assert config.getboolean("PLATFORMS", "Creatronix") is False