Python ConfigParser: A Comprehensive Guide ⚙️
Mastering Configuration Management in Python: A Deep Dive into the Configparser Module
Hello Jawans, 😎, today we will be learning about config files
, and conifParser
. Howzzzzz the Joshhhhhhhh …???… ⭐🤩🔥
1. What is a ConfigParser?
While coding we have multiple lines of code or variables holding different configuration values related to the application. The configuration variables can be related to either test or production environments or both. Since most of the configuration values are static so instead of hardcoding values in the code, we can make it more flexible by creating a config file where all configurations are stored and can be read and edited without disturbing the application’s code.
ConfigParser is a Python module that is used to create and read the configuration files and use them in the application development.
2. How to create a Config File?
Config files are files with .ini
extensions. The contents are organized as sections and items
. Sections are like headings and items are the ones that actually store the config values in a key-value pair format.
Let’s assume that we are connecting to a remote server, and we need to store config values such as host, port, username, password
, etc. Since there can be multiple servers that the applications connect to in such cases, we can separate each server’s config data using different sections in the config file.
[SFTP_SERVER1] # <------------------ Section
host = 10.120.98.12 # <------------------ Items (key-value)
port = 22
username = tester
password = password
[SFTP_SERVER2] # <------------------ Section
host = 10.120.98.12 # <------------------ Items (key-value)
port = 23
username = tester
password = password
So SFTP_SERVER1
and SFTP_SERVER2
are sections and host, port, username, password
are keys with their respective values.
To learn about how to connect to SFTP Servers, how to transfers file and run remote command read this article: link
Paramiko- How to transfer files with Remote System (SFTP Servers) using Python | by KIHSA Rai | Aug, 2023 | Medium
Section name must be unique.
3. How to Read Config Files
First, create a ConfigParser
object.
import configparser
configParser= configparser.ConfigParser() # Creating a configParser object
Read the config file. The filename used in the code is tempConfig.ini
filename= "tempConfig.ini"
configParser.read(filename) # Reading the config file
Use ApIs to list all defined sections in the config file.
Servers= configParser.sections() # Getting sections from config file
print (Servers) # Printing the Sections (Servers)
Use ApIs to read item defined under the sections.
Servers= configParser.sections() # Getting sections from config file
host= configParser[Servers[0]]['host'] # Getting item using the key host
port= configParser[Servers[0]]['port'] # Getting item using the key port
print(f"Host: {host} port: {port}")
Combined Code
Here I have created a class (HostConfig)
for reading a config file and also provided some API’s to read the sections
and items
defined in the config file.
import configparser
class HostConfig:
def __init__(self, configFile):
self.configFile= configFile
self.config= configparser.ConfigParser()
def readConfig(self):
# Reading the config file
self.config.read(self.configFile)
def getServers(self):
# Returning all the sections defined in the config file
return self.config.sections()
def getData(self, host, key):
# Getting the values from the particular section (host)
# by using the key
return self.config[host][key]
def getHostDetails(self, host):
# Getting all the values defined under the given section (host).
# Assuming the keys are known and fixed.
hostdata= self.config[host]
return hostdata['host'],hostdata['port'],hostdata['username'],hostdata['password']
hostConfig= HostConfig('tempConfig.ini') # creating object of HostConfig
hostConfig.readConfig() # Reading config file
Servers= hostConfig.getServers() # Getting the list of sections (servers)
print("Servers: ",Servers)
print("Server 0 data: \n",hostConfig.getHostDetails(Servers[0])) # Getting items for Section No 0
print("Server1: Host ",hostConfig.getData(Servers[1], 'host')) # Getting item for Section No 1
print("Server1: Port ",hostConfig.getData(Servers[1], 'port')) # Getting item for Section No 1
If you want another tutorial on how to create a Config file using Python.
If you want to learn how to convert a dictionary to a config file in Python.
please leave a comment.
If you like the tutorial, please clap for it and follow me.
Read my other articles
Mqttx Client with AWS Mqtt Broker | by KIHSA Rai | Dec, 2023 | Medium
Read my other tutorials on ReactJs, I’ve explained it in simple words with examples and also provided a Git Hub link for the source code.
Part 1: How to create a new React App Explained in simple steps
let’s meet again in next tutorial.
My references: configparser — Configuration file parser — Python 3.11.4 documentation