We’ll use ConfigParser module to deal with config files and see how easy it could be to generate and read configuration files. Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as .ini.,We’ll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console. This could be very new to a developer like me who has just started working on Python, so, we’ll start from scratch.,We then save the layout defined in an ini file named configurations.ini. You can give the file name of your choice where the settings would be saved. Then at last we are retrieving the content of the saved ini file and printing those on the terminal window or console.,I’ll not go into details of installing Python and configuring VS Code to run a python application and assume that you have it. If not, I can post a separate article based on a request on how to get started with Python in VS Code
Write a small code that reads hardcoded values and print those. To start with and for the sake of understanding I am first using hard coded values to print then later will fetch those from config file.
ftpUrl = "demoftp.codeteddy.com"
userName = "codeteddy"
password = "my#supersecret#password"
print("\nDisplaying FTP details\n")
print("FTP URL: " + ftpUrl)
print("FTP User Name: " + userName)
print("Password: " + password)
Displaying FTP details
FTP URL: demoftp.codeteddy.com FTP User Name: codeteddy Password: my #supersecret #password PS D: \Articles\ Python_ConfigFile\ Code >
In VS Code, create a new file named generate_config.py and import configparser module as shown below,
import configparser
If we open the configurations.ini file i.e., the newly generated file, we see the following settings in place,
[FTPSettings] ftpurl = demoftp.codeteddy.com username = codeteddy password = my #supersecret #password
Let’s add one more section to the file. But this time we’ll add it in a different way. For e.g., if we need to add a section for Logger and its corresponding settings, we can also use the following code to do so in a single code statement,
# ADD NEW SECTION AND SETTINGS
config_file["Logger"]={
"LogFilePath":"<Path to log file>",
"LogFileName" : "<Name of log file>",
"LogLevel" : "Info"
}
9th December, 2021: Initial version
ftpUrl = "demoftp.codeteddy.com"
userName = "codeteddy"
password = "my#supersecret#password"
print("\nDisplaying FTP details\n")
print("FTP URL: " + ftpUrl)
print("FTP User Name: " + userName)
print("Password: " + password)
Displaying FTP details
FTP URL: demoftp.codeteddy.com
FTP User Name: codeteddy
Password: my #supersecret #password
PS D: \Articles\ Python_ConfigFile\ Code >
import configparser
[FTPSettings] ftpurl = demoftp.codeteddy.com username = codeteddy password = my #supersecret #password
# ADD NEW SECTION AND SETTINGS
config_file["Logger"]={
"LogFilePath":"<Path to log file>",
"LogFileName" : "<Name of log file>",
"LogLevel" : "Info"
}
This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what’s found in Microsoft Windows INI files. You can use this to write Python programs which can be customized by end users easily.,The structure of INI files is described in the following section. Essentially, the file consists of sections, each of which contains keys with values. configparser classes can read and write such files. Let’s start by creating the above configuration file programmatically.,This option accepts a dictionary of key-value pairs which will be initially put in the DEFAULT section. This makes for an elegant way to support concise configuration files that don’t specify values which are the same as the documented default.,Read and parse configuration data from f which must be an iterable yielding Unicode strings (for example files opened in text mode).
[DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no
>>> import configparser >>> config = configparser.ConfigParser() >>> config['DEFAULT'] = { 'ServerAliveInterval': '45', ...'Compression': 'yes', ...'CompressionLevel': '9' } >>> config['bitbucket.org'] = {} >>> config['bitbucket.org']['User'] = 'hg' >>> config['topsecret.server.com'] = {} >>> topsecret = config['topsecret.server.com'] >>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ...config.write(configfile) ...
>>> config = configparser.ConfigParser() >>>
config.sections()[] >>>
config.read('example.ini')['example.ini'] >>>
config.sections()['bitbucket.org', 'topsecret.server.com'] >>>
'bitbucket.org' in config
True
>>>
'bytebong.com' in config
False
>>>
config['bitbucket.org']['User']
'hg' >>>
config['DEFAULT']['Compression']
'yes' >>>
topsecret = config['topsecret.server.com'] >>>
topsecret['ForwardX11']
'no' >>>
topsecret['Port']
'50022' >>>
for key in config['bitbucket.org']:
...print(key)
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>>
config['bitbucket.org']['ForwardX11']
'yes'
>>> another_config = configparser.ConfigParser() >>>
another_config.read('example.ini')['example.ini'] >>>
another_config['topsecret.server.com']['Port']
'50022' >>>
another_config.read_string("[topsecret.server.com]\nPort=48484") >>>
another_config['topsecret.server.com']['Port']
'48484' >>>
another_config.read_dict({
"topsecret.server.com": {
"Port": 21212
}
}) >>>
another_config['topsecret.server.com']['Port']
'21212' >>>
another_config['topsecret.server.com']['ForwardX11']
'no'
>>> int(topsecret['Port'])
50022
>>>
float(topsecret['CompressionLevel'])
9.0
>>> topsecret.getboolean('ForwardX11')
False
>>>
config['bitbucket.org'].getboolean('ForwardX11')
True
>>>
config.getboolean('bitbucket.org', 'Compression')
True
#!/usr/bin/env python import preprocessing mysql = { "host": "localhost", "user": "root", "passwd": "my secret password", "db": "write-math", } preprocessing_queue = [ preprocessing.scale_and_center, preprocessing.dot_reduction, preprocessing.connect_lines, ] use_anonymous = True
#!/usr/bin/env python
import databaseconfig as cfg
connect(cfg.mysql["host"], cfg.mysql["user"], cfg.mysql["password"])
{
"mysql": {
"host": "localhost",
"user": "root",
"passwd": "my secret password",
"db": "write-math"
},
"other": {
"preprocessing_queue": [
"preprocessing.scale_and_center",
"preprocessing.dot_reduction",
"preprocessing.connect_lines"
],
"use_anonymous": true
}
}
import json
with open("config.json") as json_data_file:
data = json.load(json_data_file)
print(data)
{
"mysql": {
"db": "write-math",
"host": "localhost",
"passwd": "my secret password",
"user": "root",
},
"other": {
"preprocessing_queue": [
"preprocessing.scale_and_center",
"preprocessing.dot_reduction",
"preprocessing.connect_lines",
],
"use_anonymous": True,
},
}
import json
with open("config.json", "w") as outfile:
json.dump(data, outfile)
In Python we have configparser module which can help us with creation of config files (.ini format).,I’m sure you must be aware about the importance of configuration files. Config files help creating the initial settings for any project, they help avoiding the hardcoded data.,Now if you check the working directory, you will notice config.ini file has been created, below is its content.,Creating config file in PythonReading a key from config fileUpdating a key in config file
from configparser import ConfigParser #Get the configparser object config_object = ConfigParser() #Assume we need 2 sections in the config file, let 's call them USERINFO and SERVERCONFIG config_object["USERINFO"] = { "admin": "Chankey Pathak", "loginid": "chankeypathak", "password": "tutswiki" } config_object["SERVERCONFIG"] = { "host": "tutswiki.com", "port": "8080", "ipaddr": "8.8.8.8" } #Write the above sections to config.ini file with open('config.ini', 'w') as conf: config_object.write(conf)
[USERINFO] admin = Chankey Pathak password = tutswiki loginid = chankeypathak [SERVERCONFIG] host = tutswiki.com ipaddr = 8.8 .8 .8 port = 8080
from configparser
import ConfigParser
#Read config.ini file
config_object = ConfigParser()
config_object.read("config.ini")
#Get the password
userinfo = config_object["USERINFO"]
print("Password is {}".format(userinfo["password"]))
Password is tutswiki
from configparser
import ConfigParser
#Read config.ini file
config_object = ConfigParser()
config_object.read("config.ini")
#Get the USERINFO section
userinfo = config_object["USERINFO"]
#Update the password
userinfo["password"] = "newpassword"
#Write changes back to file
with open('config.ini', 'w') as conf:
config_object.write(conf)
Last Updated : 20 Jun, 2019
Output :
['config.ini']
Sections: ['installation', 'debug', 'server']
Installation Library: '/usr/local/lib'
Log Errors debugged ? : True
Port Server: 8080
Worker Server: 32
When parsing values, methods such as getboolean() look for any reasonable value. For example, these are all equivalent.
log_errors = true
log_errors = TRUE
log_errors = Yes
log_errors = 1
The most noteworthy contrast between a config record and Python code is that, in contrast to scripts, configuration files are not executed in a top-down way. Rather, the file is read completely. On the off chance that variable substitutions are made, they are done later after the fact. For instance, it doesn’t make a difference that the prefix variable is allocated after different variables that happen to utilize it.
[installation]
library = % (prefix) s / lib
include = % (prefix) s / include
bin = % (prefix) s / bin
prefix = /usr/local
Creating a new config file,Reading a config file,Updating your config file,Read the configfile.ini
Back in the day, Microsoft Windows relied largely on initialisation files, or INI files, to configure operating system and applications settings. In Python, we have a module called configparser.py
which allows us to easily create, read and update INI files. If you don’t already have it installed, use the command
$ pip install configparser
Planned Filename: configfile.ini
[postgresql] host = localhost user = finxter1 password = myfinxterpw db = postgres [user_info] admin = David Yeoman login = finxter_freelancer password = freelancer_freedom
So here is the code that follows the steps above.
import configparser config = configparser.ConfigParser() # Add the structure to the file we will create config.add_section('postgresql') config.set('postgresql', 'host', 'localhost') config.set('postgresql', 'user', 'finxter1') config.set('postgresql', 'port', '5543') config.set('postgresql', 'password', 'myfinxterpw') config.set('postgresql', 'db', 'postgres') config.add_section('user_info') config.set('user_info', 'admin', 'David Yeoman') config.set('user_info', 'login', 'finxter_freelancer') config.set('user_info', 'password', 'freelancer_freedom') # Write the new structure to the new file with open(r "C:\PythonTutorials\configfile.ini", 'w') as configfile: config.write(configfile)
Here is the code you’d use when connecting using your INI file.
import sqlalchemy import configparser #Read config.ini file config_obj = configparser.ConfigParser() config_obj.read("C:\PythonTutorials\configfile.ini") dbparam = config_obj["postgresql"] useinfo = config_obj["user_info"] # set your parameters for the database connection URI using the keys from the configfile.ini user = dbparam["user"] password = dbparam["password"] host = dbparam["host"] port = int(dbparam["port"]) dbase = dbparam["db"] # Create the URI and the engine with the URI in the following format # postgresql: //username:password@hostname:port/database # so we pass the URI the parameters established above conn = sqlalchemy.create_engine('postgresql://user:password@host:port/dbase', encoding = 'utf8') # Connect to the database connection = conn.raw_connection()
As you can see we needed to use configparser
to create an object, then read the configfile.ini that we created earlier. Having read the configfile.ini we passed each section of the file to separate variables, in this case, the ‘postgresql’
parameters to dbparam
and the ‘user_info’
parameters to useinfo.
#Read config.ini file
config_obj = configparser.ConfigParser()
config_obj.read("C:\PythonTutorials\configfile.ini")
dbparam = config_obj["postgresql"]
useinfo = config_obj["user_info"]