Install the Yaml Package for Python

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:

Or

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

Windows:

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:

Windows:

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):

(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:

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

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:

We can then install ruamel.yaml package using:

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.

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)

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.

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).