Configuration Files in Python

Posted by

Do you even need config files?

If a developer has a module in their project that connects with an FTP server to download some files, then the developer would write a method to connect to FTP via FTP URL and use credentials like username and password for successful connection. If the developer uses these credentials in the code and hardcodes them in their code file and deploys the application, it could work fine, job done! Now, imagine that after two months the password for that FTP site is changed and the developer must again update that password in your application to make sure it does not break the existing functionality of the FTP connection. Now in this scenario, the developer would again change the code and redeploy it. Here, a developer must for a small configuration change, take the latest of the code, do the required change, make sure nothing else breaks, re-deploy the application and then test it. And this could be a repetitive task after 2~3 months when some configuration again changes. What if the application has a configuration file that has key-value pairs as “User”: “<username>”, “Password”: “password” and whenever some change is needed only that file is touched and configurations are updated rather than digging into the actual code.

Why config files?

Coming from a .Net background, I found it a bit challenging in figuring out how a developer can have a configuration file in the Python application which could be used to read setting values and one does not have to even touch the code to update or save settings. Config files are used to store key-value pairs or some configurable information that could be read or accessed in the code and at some point, of time. If that configuration changes, developers can just change the configuration in that config file and not worry about changing the code as it may require recompiling the code and deploying it. Not only this but using config files make your settings and code more reusable and keeps the settings information at a centralized location and segregated. Of course, sensitive information like passwords, secrets, and certificates should be kept more secure, maybe in cloud vaults. But basic settings used in the application could be part of a configuration file.

Microsoft.Net vs Python config files

Microsoft .Net applications sometimes by default provide files like appSettings, web.config, app.config when you start with a project template, based on what kind of application you are creating. These files serve the purpose of storing settings information.

Python applications do not by default provide a settings file, however, you can use an existing one and modify it. But the best way is to create your own from scratch and use it as needed.

Getting Started

In this article, we’ll have a walkthrough of how to create a config file, add a configuration, update a configuration, delete a configuration, and read a configuration in a python application.

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 a 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.


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

Create a Config File

Launch VS Code and create a file

Open VS Code and create a new file and name it

Start from Hardcoding.

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 the config file.

ftpUrl = ""
userName = "codeteddy"
password = "my#supersecret#password"

print("\nDisplaying FTP details\n")

print("FTP URL: " + ftpUrl)
print("FTP User Name: " + userName)
print("Password: " + password)

Run the code.

Now run the code to print the details. At the corner, there should be an icon to run your code and display the output on the terminal.

So, output is,

Displaying FTP details
FTP User Name: codeteddy
Password: my#supersecret#password
Our goal is to make sure that the output remains the same, but the values of FTP settings are read from a config file.

Code to generate config file.

In VS Code, create a new file named and import configparser module as shown below,

import configparser

Now put the following code into the file,

import configparser

So, we import configparser module that contains methods and classes which could be used to define a layout of the config file that we need. We create an object of Configparser and name it config_file. A config file can have sections against which the details would be stored i.e., FTP Settings or Logger settings etc. So, we created one section named “FTPSettings” and added key-value pairs to that section via config_file.set method which takes the first argument as section name, second as key, and third as value. In our case section name is “FTPSettings” and three keys would be ftpUrl, userName and password.

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.

Once you save this file and hit on the run, it will generate a configurations.ini file.

Generated file.

If we open the configurations.ini file i.e., the newly generated file, we see the following settings in place,

ftpurl =
username = codeteddy
password = my#supersecret#password

It contains the section FTPSettings and our FTP settings as key-value pairs under it. Each time the code for generate_config is run, it will create a fresh config file with the layout as defined in the code file.

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,

        "LogFilePath":"<Path to log file>",
        "LogFileName" : "<Name of log file>",
        "LogLevel" : "Info"

Now, the looks like as follows with the newly added section,

import configparser

config_file = configparser.ConfigParser()

config_file.set("FTPSettings", "ftpUrl", "")
config_file.set("FTPSettings", "userName", "codeteddy")
config_file.set("FTPSettings", "password", "my#supersecret#password")

        "LogFilePath":"<Path to log file>",
        "LogFileName" : "<Name of log file>",
        "LogLevel" : "Info"

with open(r"configurations.ini", 'w') as configfileObj:

print("Config file 'configurations.ini' created")

read_file = open("configurations.ini", "r")
content =
print("Content of the config file are:\n")

When we run the code, we see that our config file is updated with these new details,

ftpurl =
username = codeteddy
password = my#supersecret#password

logfilepath = <Path to log file>
logfilename = <Name of log file>
loglevel = Info

So, we can use either of the ways i.e. add_section() and set method or object initializer way to create a section and add settings to it.

Adding/Updating/Deleting configuration file settings

We can add or update or delete the settings in the config file via code as well.

Adding/Updating settings in the config file

For e.g. If we need to update the “loglevel” from “Info” to “Debug”, or we need to add a new setting to the Logger section. Either you can update the setting in the same file and run it or you can write code to read the existing configuration file and update new settings. For e.g. I created a new python file named and added the following code to it,

import configparser

config_file = configparser.ConfigParser()

# READ CONFIG FILE"configurations.ini")
with open("configurations.ini","w") as file_object:
print("Config file 'configurations.ini' is updated")
print("Updated file settings are:\n")

Here in this code, we first import, create the object of configparser. We read the existing configuration file and then set the LogLevel from the Logger section to “Debug”. Likewise, we added one more setting under the Logger section via update method by this code:


This code adds a new setting named “Format” with value as “(message)” under the Logger section. In the end, we again save the file and then again read it to display the saved information.

Now when you run the code, the output is

Config file 'configurations.ini' is updated
Updated file settings are:
ftpurl =
username = codeteddy
password = my#supersecret#password
logfilepath = <Path to log file>
logfilename = <Name of log file>
loglevel = Debug
format = (message)

So, these are the ways, you can update the ini file.

Deleting the settings

We can remove the settings from config files using remove_option() and remove_section() module in configparser module. remove_option() is used to delete a field (i.e., key value pair) from any section and remove_section() is used to remove a complete section of the config file.

config_file.remove_option('Logger', 'Format')

Reading the configuration file in Python

Now that the file is created, and we have learned how to generate and update the config file when needed. Let’s visit the last section of this article i.e., reading the values from a config file in our main class. This is the most simple and straightforward part.

Let’s make it a bit organized and reusable. Let’s add a helper file in our VS Code workspace and name it and add the following code to it,

import configparser

# Method to read config file settings
def read_config():
    config = configparser.ConfigParser()'configurations.ini')
    return config

Now this helper method code will serve as reusable method wherever we need the configurations.

Now. Go to the file and import this helper file as

Now, in the, replace the hardcoded pieces with reading the settings from config file as,

import helper

config = helper.read_config()

ftpUrl = config['FTPSettings']['ftpUrl']
userName = config['FTPSettings']['userName']
password = config['FTPSettings']['password']

print("\nDisplaying FTP details\n")

print("FTP URL: " + ftpUrl)
print("FTP User Name: " + userName)
print("Password: " + password)

And run the code. We’ll see that our output is the same as when it was reading the values from the hardcoded code.

Now it reads from the config file.

Complete Source Code

Leave a Reply

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

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.