Skip to main content

Extending PyYAML with a custom constructor for including YAML files within YAML files

Project description


CircleCI Documentation Status GitHub tag PyPI PyPI - License PyPI - Format PyPI - Status PyPI - Python Version PyPI - Implementation Codacy Badge Codacy Badge

Extending PyYAML with a custom constructor for including YAML files within YAML files.


pip install pyyaml-include


Consider we have such YAML files:

├── 0.yaml
└── include.d
    ├── 1.yaml
    └── 2.yaml
  • 1.yaml 's content:

    name: "1"
  • 2.yaml 's content:

    name: "2"

To include 1.yaml, 2.yaml in 0.yaml, we shall first add YamlIncludeConstructor to PyYAML's loader, then write !include tag in 0.yaml and load it:

import yaml
from yamlinclude import YamlIncludeConstructor


with open('0.yaml') as f:
    data = yaml.load(f)


Include files by name

  • On top level:

    If 0.yaml was:

    !include include.d/1.yaml

    We'll get:

    {"name": "1"}
  • In mapping:

    If 0.yaml was:

    file1: !include include.d/1.yaml
    file2: !include include.d/2.yaml

    We'll get:

        name: "1"
        name: "2"
  • In sequence:

    If 0.yaml was:

      - !include include.d/1.yaml
      - !include include.d/2.yaml

    We'll get:

      - name: "1"
      - name: "2"

ℹ Note: File name can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../img/banner.gif).

Include files by wildcards

File name can contain shell-style wildcards. Data loaded from the file(s) found by wildcards will be set in a sequence.

If 0.yaml was:

files: !include include.d/*.yaml

We'll get:

  - name: "1"
  - name: "2"

ℹ Note:

  • For Python>=3.5, if recursive argument of !include YAML tag is true, the pattern “**” will match any files and zero or more directories and subdirectories.
  • Using the “**” pattern in large directory trees may consume an inordinate amount of time because of recursive search.

In order to enable recursive arugment, we shall write the !include tag in Mapping or Sequence mode:

  • Arguments in Sequence mode:

    !include [tests/data/include.d/**/*.yaml, true]
  • Arguments in Mapping mode:

    !include {pathname: tests/data/include.d/**/*.yaml, recursive: true}


liu xue yan



Date: 2019-01--7

  • Change:

    • rename: TAG ==> DEFAULT_TAG_NAME
    • add: encoding argument
  • Fix:

    • A wrong logging text format
  • Misc:

    • add: .pylintrc


Date: 2018-12-04

  • New Feature:

    • Add base_dir argument
  • Misc:

    • Add some new unit-test
    • Add Python3.7 in circleci


Date: 2018-07-11

  • Add:

    • encoding argument
  • Bugix:

    • encoding error if non-ascii characters on non-utf8 os.


Date: 2018-07-03

  • Add:

    • Old Python2.6 and new Python3.7 compatibilities

    • class method add_to_loader_class

      A class method to add the constructor itself into YAML loader class

    • Sphinx docs

  • Change:

    • Rename module file to

    • Rename class data member DEFAULT_TAG to TAG


Date: 2018-06-08

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pyyaml_include-1.0.4-py2.py3-none-any.whl (18.3 kB) Copy SHA256 hash SHA256 Wheel py2.py3
pyyaml-include-1.0.4.tar.gz (34.4 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page