Configuration files are used to store settings, preferences, and credentials for applications. Python provides multiple ways to read and write config files, making it easy to manage application settings.
1. Why Use Config Files?
✅ Separation of code and settings – No need to hardcode values in scripts.
✅ Easier maintenance – Modify settings without changing code.
✅ Environment-specific settings – Different configurations for development, testing, and production.
✅ Secure storage – Avoid storing sensitive information directly in scripts.
2. Config File Formats
Python supports various config file formats:
🔹 INI (configparser
module) – Simple key-value pairs grouped into sections.
🔹 JSON (json
module) – Lightweight and widely used for web applications.
🔹 YAML (pyyaml
module) – Human-readable and supports complex structures.
🔹 TOML (toml
module) – Used for Python project settings (pyproject.toml
).
🔹 ENV Files (dotenv
module) – Used for environment variables.
1. INI Configuration Files (configparser
)
INI files contain sections with key-value pairs:
1.1 Sample config.ini
File
[database]
host = localhost
port = 5432
username = admin
password = secret
[server]
debug = True
log_level = INFO
1.2 Reading INI Files
import configparser
config = configparser.ConfigParser()
config.read("config.ini")
# Access values
db_host = config["database"]["host"]
db_port = config.getint("database", "port")
debug_mode = config.getboolean("server", "debug")
print(f"Database Host: {db_host}")
print(f"Port: {db_port}")
print(f"Debug Mode: {debug_mode}")
1.3 Writing to an INI File
config["api"] = {"base_url": "https://api.example.com", "timeout": "30"}
with open("config.ini", "w") as configfile:
config.write(configfile)
2. JSON Configuration Files (json
Module)
JSON is a widely used format for configuration files in web and APIs.
2.1 Sample config.json
File
{
"database": {
"host": "localhost",
"port": 5432,
"username": "admin",
"password": "secret"
},
"server": {
"debug": true,
"log_level": "INFO"
}
}
2.2 Reading JSON Files
import json
with open("config.json", "r") as file:
config = json.load(file)
print(config["database"]["host"]) # Output: localhost
print(config["server"]["debug"]) # Output: True
2.3 Writing JSON Files
config["api"] = {"base_url": "https://api.example.com", "timeout": 30}
with open("config.json", "w") as file:
json.dump(config, file, indent=4)
3. YAML Configuration Files (pyyaml
Module)
YAML is human-friendly and supports nested structures.
3.1 Sample config.yaml
File
database:
host: localhost
port: 5432
username: admin
password: secret
server:
debug: true
log_level: INFO
3.2 Reading YAML Files
import yaml
with open("config.yaml", "r") as file:
config = yaml.safe_load(file)
print(config["database"]["host"]) # Output: localhost
print(config["server"]["debug"]) # Output: True
3.3 Writing YAML Files
config["api"] = {"base_url": "https://api.example.com", "timeout": 30}
with open("config.yaml", "w") as file:
yaml.dump(config, file, default_flow_style=False)
Install pyyaml
if not installed:
pip install pyyaml
4. TOML Configuration Files (toml
Module)
TOML is commonly used for Python project settings.
4.1 Sample config.toml
File
[database]
host = "localhost"
port = 5432
username = "admin"
password = "secret"
[server]
debug = true
log_level = "INFO"
4.2 Reading TOML Files
import toml
config = toml.load("config.toml")
print(config["database"]["host"]) # Output: localhost
print(config["server"]["debug"]) # Output: True
4.3 Writing TOML Files
config["api"] = {"base_url": "https://api.example.com", "timeout": 30}
with open("config.toml", "w") as file:
toml.dump(config, file)
Install toml
if not installed:
pip install toml
5. Environment Variables (dotenv
Module)
ENV files store environment-specific settings.
5.1 Sample .env
File
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USER=admin
DATABASE_PASS=secret
5.2 Reading .env
Files
from dotenv import load_dotenv
import os
load_dotenv() # Load environment variables from .env file
db_host = os.getenv("DATABASE_HOST")
db_port = os.getenv("DATABASE_PORT")
print(f"Database Host: {db_host}")
print(f"Port: {db_port}")
📌 Install python-dotenv
if not installed:
pip install python-dotenv
6. Choosing the Right Format
Format | Best For | Pros | Cons |
---|---|---|---|
INI | Simple settings | Easy to read | Limited nesting |
JSON | Web & API settings | Widely supported | No comments allowed |
YAML | Complex configs | Human-friendly | Requires extra library |
TOML | Python projects | Clear syntax | Less common |
ENV | Environment settings | Easy to use | Flat structure |