Start using config files!

Start using config files!

Table of contents

There are many ways you can pass configurations or parameters to modify the behavior of your Python program. But why exactly do you need to pass these configurations? Let's say you write a Python script to connect to a database. In order to connect to databases you need credentials and other details (such as database name, URL, port, etc). One way to connect to the database would be to hardcode these to the code itself.

host = "localhost"
port = "5432"
username = "user"
password = "password"
database = "my_database"

So what is the problem with hardcoding these variables into the code? The problem arises because you might need to change these parameters when you move your database to a different location, or when the credentials change. Also, hardcoding credentials is not a good practice in itself. In order to change these variables, you might need to change the code itself, which is a bad coding practice.

configparser

Here is where we can introduce the configparser to our code. It is a built-in module in Python that allows us to pass these parameters to the code. Instead of hardcoding the parameters like this, we create a separate file (config.ini) file and store them.

#config.ini
[database]
host = localhost
port = 5432
username = user
password = password
database = my_database

Now, we can use the configparser module to read this file.

import configparser

config = configparser.ConfigParser()
config.read('config.ini')

host = config.get('database', 'host')
port = config.getint('database', 'port')
username = config.get('database', 'username')
password = config.get('database', 'password')
database = config.get('database', 'database')

This way, you can change the config.ini file whenever you want to change the variables in the code without touching the code.

Of course, these config files can get large. Suppose you have multiple databases. Writing configurations for all these databases in one file sounds like a terrible idea. So you can always make use of sections in config files or just use different configuration files.

Personally, I do not prefer using configparser. Firstly, it does not inherently support complex data structures such as lists. Secondly, this file may not be parsable with other languages (probably because it is a legacy configuration storage method). I prefer using YAML or JSON over the .ini format. For better readability, my preference has been to go to the YAML file lately.

YAML

YAML stands for Yet Another Markup Language. It is similar to JSON in many ways and stores data in unstructured formats. When I say unstructured formats, I mean it is capable of storing configurations in a nested format. Using YAML to store configuration is just as easy as it was in .ini format.

#config.yaml file
database:
  host: host
  port: 5432
  username: user
  password: password
  database_name: database

For this example, I use the same example of a database configuration. To read this file, first, we must install a pyyaml package:

pip install pyyaml

Now, we read the YAML file.

import yaml

# Read the YAML configuration file
with open('config.yaml', 'r') as f:
    config = yaml.safe_load(f)

# Retrieve the database connection details from the configuration
db_config = config['database']


host = db_config['host']
port = db_config['port ']
username = db_config['username ']
password = db_config['password ']
database = db_config['database ']