Why Write Configuration Files and How to Parse INI, JSON, TOML, and YAML in Python
This article explains the purpose of configuration files, compares common formats such as INI, JSON, TOML, and YAML, and demonstrates how to read each of them in Python using built‑in libraries or third‑party packages while highlighting their advantages and pitfalls.
During development we often need to store fixed parameters or constants; placing them in a dedicated configuration file keeps the core code tidy and avoids repetition across modules. While a simple .py file (e.g., settings.py or config.py ) can be imported within a Python project, sharing configuration with non‑Python platforms requires a language‑agnostic format such as INI, JSON, TOML, YAML, or XML.
INI is an early Windows configuration format consisting of sections, keys, and values. A typical INI file looks like:
<code>[localdb]
host = 127.0.0.1
user = root
password = 123456
port = 3306
database = mysql
</code>Python’s built‑in configparser module can read it:
<code>from configparser import ConfigParser
cfg = ConfigParser()
cfg.read("/Users/Bobot/db.ini")
items = cfg.items("localdb")
</code>Values are returned as strings, which can be converted to a dictionary and unpacked for database connections.
JSON is a widely used data‑exchange format that also serves as a configuration file. Python’s json module provides load() and loads() for reading files or strings:
<code>{"localdb":{ "host":"127.0.0.1", "user":"root", "password":"123456", "port":3306, "database":"mysql"}}
</code>Reading the file:
<code>import json, pprint
with open('/Users/Bobot/db.json') as j:
cfg = json.load(j)['localdb']
pprint(cfg)
</code>JSON’s strict syntax disallows comments and can become unwieldy with deeply nested structures.
TOML was proposed by Tom Preston‑Werner in 2013. It resembles INI but supports richer data types such as timestamps, booleans, and arrays, and its syntax mirrors native Python literals. Example content:
<code>[mysql]
host = "127.0.0.1"
user = "root"
port = 3306
database = "test"
[mysql.parameters]
pool_size = 5
charset = "utf8"
[mysql.fields]
pandas_cols = [ "id", "name", "age", "date"]
</code>Install the parser with pip install toml and load it:
<code>import toml, os, pprint
cfg = toml.load(os.path.expanduser("~/Desktop/config.toml"))
pprint(cfg)
</code>YAML (or YML) is another popular format, used for files like docker-compose.yml . Python’s PyYAML library can parse it, but using yaml.load() is unsafe because it may execute arbitrary code. The recommended function is yaml.safe_load() :
<code>import yaml, os, pprint
with open(os.path.expanduser("~/config.yaml"), "r") as f:
cfg = yaml.safe_load(f)
pprint(cfg)
</code>YAML’s specification is extensive (over 80 pages), which explains its expressive power and occasional complexity.
In summary, the configuration formats increase in complexity as follows: ini < json ≈ toml < yaml . Each has its own strengths and trade‑offs, so choose the one that best fits your project’s requirements and team workflow. Other formats such as .cfg , .properties , or even a plain .py file can also be used when appropriate.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.