YAML is a human-readable data-serialization language. It is frequently used for configuration files and in programs that store or send data.
Python doesn’t support the YAML data format out of the box. We will require a third-party library to serialize Python objects into YAML and vice versa. PyYAML and ruamel.yaml packages are commonly used for this purpose.
In this article, we will learn how to install these packages, and as a bonus, we will use these libraries to load a YAML file.
Installing PyYAML
PyYAML implements YAML 1.1 specifications since 2004. In this section, we discuss some methods for installing the PyYAML library.
Method 1: Installing PyYAML using pip
PyYAML can be installed using pip by executing the following line on the command line:
Unix/macOS:
pip3 install pyyaml
Or
python3 -m pip install pyyaml
In some cases, libyaml is still required to build the C extension (on mac); therefore, mac users should install libyaml before using pip to install the PyYAML package as follows
brew install libyaml
python -m pip install pyyaml
Windows:
py -m pip install pyyaml
Method 2: Installing PyYAML from the source
First, download PyYAML GZ compressed source tarball (.tar.gz) from the link https://pypi.org/project/PyYAML/#files (At the time of writing this article, PyYAML v6.0 is available. When I download the file I get PyYAML-6.0.tar.gz)
Once the tarball has been downloaded, you can install the package with any of the following commands:
Linux/macOS:
python3 -m pip install PyYAML-***.tar.gz
Windows:
py -m pip install PyYAML-***.tar.gz
If you run into problems with that, you can proceed as follows:
Extract the tarball. Use the inbuilt extraction tool to extract the tarball, or you can use the tar command as follows (tar is available on Linux, Windows, and macOS):
tar -xf PyYAML-***.tar.gz
(In my case, I have to run tar -xf PyYAML-6.0.tar.gz)
Lastly, we install the package using the setup that comes with the source files. To do that, go to the extracted directory PyYAML-*** and run:
Linux/macOS/Windows:
python3 setup.py install
Method 3: Using Package Manager (for Linux Users)
If you want to install the PyYAML package for system-wide access in Linux, you can use a package manager, like aptitude (apt) for Debian-based distros or yum for Centos:
Step 1: Update local repositories
sudo apt update
sudo yum makecache –refresh
Step 2: Install dependencies
sudo apt install libyaml-dev
sudo yum -y install libyaml-devel
Step 3: Installing PyYAML
sudo apt install python3-yaml
sudo yum install python3-yaml
Install ruamel.yaml library
Unlike PyYAML, ruamel.yaml implements more recent YAML 1.2 specifications. This package can be installed using pip or package manager just like PyYAML
Method 1: Using pip
Before installing ruamel.yaml, ensure that you have recent versions of pip, setuptools, and wheel by running the following on the command line:
pip3 install -U pip setuptools wheel
We can then install ruamel.yaml package using:
pip3 install ruamel.yaml
Method 2: Installing ruamel.yaml using a package manager
To do that, update repositories and install the package. I am running Debian; therefore, I will use the apt package manager.
sudo apt update
sudo apt install python3-ruamel.yaml
Bonus: Loading the YAML file using the two packages discussed
Once the two libraries have been installed, we can use them to parse the YAML file into Python objects. Here is an example of YAML formatted data. Save them into “file_eq.yaml” file.
File: file_eq.yaml
# First level data
Groceries:
# second level indentation
# a list
fruits: [apple, banana, orange]
# another way of representing a list
vegetables:
- tomato
- cucumber
- onion
# Indented block key-value pairs
People:
# second level of indented block
Smith:
full name: John Smith
age: 33
# Another way of doing it
# second level Inline block for key-value pairs
Bob: {full name: Bob Williams, age: 26}
Loading file_eq.yaml file using PyYAML (the package is imported as yaml)
import yaml
# Loading the data from yaml file
with open('file_eq.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
# the data is parsed into Python dictionary
print(type(data))
# printing the data - Python dictionary
print(data)
# accessing values using keys
print(data["Groceries"]["fruits"])
Output:
<class 'dict'>
{'Groceries': {'fruits': ['apple', 'banana', 'orange'], 'vegetables': ['tomato', 'cucumber', 'onion']}, 'People': {'Smith': {'full name': 'John Smith', 'age': 33}, 'Bob': {'full name': 'Bob Williams', 'age': 26}}}
['apple', 'banana', 'orange']
The output shows that the YAML data is parsed into a Python dictionary. From there, we can use it as an ordinary Python dictionary. The code snippet below shows how we can load the same YAML file using ruamel.yaml.
from ruamel.yaml import YAML
# initialization of the YAML object
yaml=YAML(typ='safe') # default, if not specfied, is 'rt' (round-trip)
# open the yaml file
with open('file_eq.yaml') as fp:
data = yaml.load(fp)
# data is passed into Python dictionary
print(type(data))
# printing out the resulting dictionary
print(data)
# accessig values on the dictionary
print(data["People"]["Smith"])
Output:
<class 'dict'>
{'Groceries': {'fruits': ['apple', 'banana', 'orange'], 'vegetables': ['tomato', 'cucumber', 'onion']}, 'People': {'Smith': {'full name': 'John Smith', 'age': 33}, 'Bob': {'full name': 'Bob Williams', 'age': 26}}}
{'full name': 'John Smith', 'age': 33}
Conclusion and references
We discussed two Python packages that can be used to serialize and parse YAML data – PyYAML and ruamel.yaml.
The former is the most popular, and the latter is not so popular at the moment but uses more current YAML specifications (formatting rules).
You can read more about the packages on their documentation: https://pyyaml.org/wiki/PyYAMLDocumentation (PyYAML) and https://yaml.readthedocs.io/en/latest/overview.html (ruamel.yaml).