If you’ve ever worked with configuration files, Docker Compose, Kubernetes, or CI/CD pipelines, you’ve probably used YAML. It’s ubiquitous in modern development, and for good reason: it’s human-readable, simple, and powerful.
In this guide, you will learn how to work with Yaml files in Python. We will cover reading, writing and manipulating YAML data in practice.
🔗 You can find the code on GitHub.
Conditions
Before working with yaml in Python, you should:
Python 3.8 or later is installed
Basic Python knowledge: Variables, data types, functions, and control structures
Understanding of data structures: Dictionaries, Lists, and Household Data Structures
File Handling Basics: Reading and writing files in Python
Command line orientation: Running the Python script and installing the package with it
pip
You will also need to install pyyaml Albarat:
pip install pyyaml
Table of Contents
What is Yamel and why should you care?
Yamal (Yaml Markup Language) is a data serialization format designed to be easy to read and write. Think of it as JSON’s more readable cousin. 🙂
Here is the same data in JSON and YAML:
JSON:
{
"database": {
"host": "localhost",
"port": 5432,
"credentials": {
"username": "admin",
"password": "secret"
}
}
}
Yamel:
database:
host: localhost
port: 5432
credentials:
username: admin
password: secret
The YAML version is cleaner and easier to read, especially for configuration files.
How to read yaml files
Let’s say you have a configuration file for a web application. We’ll make a simple one config.yaml file and learn how to read it in Python.
First, we understand what we are trying to do. You have configuration data stored in a YAML file, and you want to load it into Python so you can use it in your application. Here’s how you can do it:
import yaml
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
print(config('database')('host'))
Output:
localhost
Here’s what’s happening in this code:
We import
yamlModuleThen we open the file using the context manager (
withstatement), which automatically closes the file by doing our work.We use
yaml.safe_load()To parse the contents of yaml into a python dictionary so we can access the data like any python dictionary.
âš Note that you should Always use yaml.safe_load() Instead yaml.load(). safe_load() The function protects you from the dangers of executing arbitrary code. Unless you have a specific reason (and you probably don’t), stick with it safe_load().
How to write yaml files
Now we go in the opposite direction. You have Python data structures and want to save them as YAML files. This is useful when you are creating configuration files or exporting data.
Here’s a practical example: Imagine you’re building a tool that generates configuration files for different environments (development, staging, production):
import yaml
config = {
'database': {
'host': 'localhost',
'port': 5432,
'name': 'myapp_db',
'credentials': {
'username': 'admin',
'password': 'secret123'
}
},
'server': {
'host': '0.0.0.0',
'port': 8000,
'debug': True
},
'features': {
'enable_cache': True,
'cache_ttl': 3600
}
}
with open('generated_config.yaml', 'w') as file:
yaml.dump(config, file, default_flow_style=False)
Let’s break down what’s going on:
We create a nested python dictionary with our layout.
We open a file in write mode (
'w')We use
yaml.dump()To convert a Python dictionary to YAML format and write it to a file.default_flow_style=FalseThe parameter ensures that the output uses block style (readable, indented format) instead of inline style.
The result generated_config.yaml The file will be properly formatted and ready for use.
How to work with lists in yaml
Yaml handles lists beautifully, and they are common in configuration files. Let’s look at a practical example: managing a list of API endpoints.
Suppose you are building a microservices application and need to configure multiple service endpoints. Here’s how you’ll work with this data:
import yaml
services_config = {
'services': (
{
'name': 'auth-service',
'url': '',
'timeout': 30
},
{
'name': 'payment-service',
'url': '',
'timeout': 60
},
{
'name': 'notification-service',
'url': '',
'timeout': 15
}
),
'retry_policy': {
'max_attempts': 3,
'backoff_seconds': 5
}
}
with open('services.yaml', 'w') as file:
yaml.dump(services_config, file, default_flow_style=False, sort_keys=False)
with open('services.yaml', 'r') as file:
loaded_services = yaml.safe_load(file)
for service in loaded_services('services'):
print(f"Service: {service('name')}, URL: {service('url')}")
Output:
Service: auth-service, URL:
Service: payment-service, URL:
Service: notification-service, URL:
This code helps us understand some important concepts.
We can nest and nest dictionaries independently in our Python data structure. sort_keys=False The parameter preserves the order of the keys as we defined them. When we read back in Yaml, we can iterate over lists just like any Python list. Data structures in Python are similar to structures in Yaml.
Create a yaml config manager
Let’s wrap everything up with a practical example. We’ll create a simple Configuration Manager class that will handle environment-specific configurations (a common requirement in real projects).
import yaml
import os
class ConfigManager:
def __init__(self, config_dir='configs'):
self.config_dir = config_dir
self.config = {}
def load_config(self, environment='development'):
"""Load configuration for a specific environment"""
config_file = os.path.join(self.config_dir, f'{environment}.yaml')
try:
with open(config_file, 'r') as file:
self.config = yaml.safe_load(file)
print(f"✓ Loaded configuration for {environment}")
return self.config
except FileNotFoundError:
print(f"✗ Configuration file not found: {config_file}")
return None
except yaml.YAMLError as e:
print(f"✗ Error parsing YAML: {e}")
return None
def get(self, key_path, default=None):
"""Get a configuration value using dot notation"""
keys = key_path.split('.')
value = self.config
for key in keys:
if isinstance(value, dict) and key in value:
value = value(key)
else:
return default
return value
def save_config(self, environment, config_data):
"""Save configuration to a file"""
config_file = os.path.join(self.config_dir, f'{environment}.yaml')
os.makedirs(self.config_dir, exist_ok=True)
with open(config_file, 'w') as file:
yaml.dump(config_data, file, default_flow_style=False)
print(f"✓ Saved configuration for {environment}")
This ConfigManager The class shows you how to create practical utilities:
The beginning: We set up a directory for configuration files.
Loading:
load_config()The method reads environment-specific YAML files with appropriate error handling.Data access:
get()The method lets you access nested values ​​using the dot notation (eg'database.host')saving:
save_config()The method writes configuration data to YAML files.
This is the kind of pattern you can actually use in projects. You can extend it further by adding validation, environment variable overrides, or configuration. Here’s how you can use it ConfigManager The class we coded is:
if __name__ == '__main__':
config_mgr = ConfigManager()
dev_config = {
'database': {
'host': 'localhost',
'port': 5432,
'name': 'dev_db'
},
'api': {
'base_url': 'http://localhost:8000',
'timeout': 30
}
}
config_mgr.save_config('development', dev_config)
config_mgr.load_config('development')
print(f"Database host: {config_mgr.get('database.host')}")
print(f"API timeout: {config_mgr.get('api.timeout')}")
Running the above code should give you the following output:
✓ Saved configuration for development
✓ Loaded configuration for development
Database host: localhost
API timeout: 30
The result
Yaml is a powerful tool in your developer toolkit. This comes in handy when you’re building applications, defining CI/CD pipelines, or working with infrastructure as code.
In this article, you learned how to work with Yaml files in Python. You can read configuration files, write data to YAML format, handle lists and nested structures, and build like functional utilities. ConfigManager We coded.
Start small. Try replacing the JSON config file in one of your projects with YAML. You’ll quickly appreciate how much more readable it is, and you’ll be comfortable working with Yaml across tools and platforms that use it.
Happy coding!