yamllint

raw JSON →
1.38.0 verified Tue May 12 auth: no python install: verified

yamllint is a linter for YAML files written in Python, version 1.38.0. It checks not only for syntax validity but also for common weirdnesses like key repetition and cosmetic issues such as line length, trailing spaces, and indentation. The project is actively maintained with regular releases.

pip install yamllint
error yamllint: command not found
cause The directory where pip installed the `yamllint` executable is not included in your system's PATH environment variable.
fix
Run yamllint using python -m yamllint <file.yaml> or add the pip binaries directory (e.g., ~/.local/bin on Linux/macOS) to your system's PATH.
error yamllint: error: Invalid config file: <path/to/.yamllint>
cause The specified `.yamllint` configuration file contains invalid YAML syntax, such as duplicate keys, incorrect indentation, or an unsupported structure.
fix
Review the .yamllint file for syntax errors, ensuring it's valid YAML and conforms to the yamllint configuration specification.
error yamllint: error: No such option: --<some-option>
cause You are attempting to use a command-line option that does not exist, is misspelled, or is deprecated for your installed `yamllint` version.
fix
Run yamllint --help to see the list of valid options, or consult the yamllint documentation for the correct command-line arguments.
error ModuleNotFoundError: No module named 'yamllint'
cause The `yamllint` Python package is not installed in the active Python environment where your script or application is attempting to import it.
fix
Install the package using pip: pip install yamllint.
breaking yamllint has progressively dropped support for older Python versions. As of 1.38.0, Python 3.9 is no longer supported, and Python 3.8 was dropped in 1.36.0. Users on older Python environments will need to upgrade their Python version or use an older yamllint version.
fix Upgrade Python to 3.10+ or pin `yamllint` to an older compatible version.
gotcha By default, the `yamllint` CLI exits with code 1 only if errors are found, not warnings. This can lead to CI/CD pipelines passing silently even with warnings. To make warnings also cause a non-zero exit code, use the strict mode.
fix Run `yamllint` with the `--strict` (or `-s`) option in CI/CD environments: `yamllint --strict .`
gotcha yamllint automatically discovers and loads configuration files named `.yamllint`, `.yamllint.yaml`, or `.yamllint.yml` in the current working directory or any parent directory. This implicit loading can lead to unexpected linting rules being applied if a configuration file exists upstream that you are unaware of or did not intend to use.
fix Explicitly specify a configuration file using the `-c` option (`yamllint -c /path/to/myconfig.yaml file.yaml`) or disable automatic config search for specific use cases. Be aware of your project's directory structure for `.yamllint` files.
breaking The API for `linter.run` experienced a change in version 1.35.0 regarding how file ignoration was handled, which was then reverted in 1.35.1. If you programmatically interact with `linter.run` and rely on specific file path handling or ignoration behavior, this version range could introduce unexpected results.
fix Avoid `yamllint` version 1.35.0 if programmatic file ignoration is critical; upgrade to 1.35.1 or newer, or downgrade to 1.34.x or older.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.17s 20.8M
3.10 alpine (musl) - - 0.18s 20.8M
3.10 slim (glibc) wheel 1.6s 0.11s 22M
3.10 slim (glibc) - - 0.12s 22M
3.11 alpine (musl) wheel - 0.37s 23.0M
3.11 alpine (musl) - - 0.44s 23.0M
3.11 slim (glibc) wheel 1.7s 0.33s 24M
3.11 slim (glibc) - - 0.32s 24M
3.12 alpine (musl) wheel - 0.27s 14.9M
3.12 alpine (musl) - - 0.30s 14.9M
3.12 slim (glibc) wheel 1.6s 0.29s 16M
3.12 slim (glibc) - - 0.32s 16M
3.13 alpine (musl) wheel - 0.24s 14.6M
3.13 alpine (musl) - - 0.27s 14.5M
3.13 slim (glibc) wheel 1.6s 0.26s 16M
3.13 slim (glibc) - - 0.29s 16M
3.9 alpine (musl) wheel - 0.14s 20.3M
3.9 alpine (musl) - - 0.18s 20.3M
3.9 slim (glibc) wheel 2.1s 0.15s 21M
3.9 slim (glibc) - - 0.15s 21M

This quickstart demonstrates how to programmatically lint a YAML string using `yamllint`. It creates a `YamlLintConfig` and then uses `linter.run` with an `io.StringIO` object to process the YAML content, iterating over any `LintProblem` objects found.

import io
from yamllint.config import YamlLintConfig
from yamllint import linter

yaml_content = """
key: value 
another_key:   with_trailing_spaces
  nested: item
"""

# Use a default configuration or load a custom one
config_string = "extends: default\nrules:\n  trailing-spaces: enable\n  indentation: enable"
conf = YamlLintConfig(content=config_string)

# Lint the YAML content from a string buffer
problems = linter.run(io.StringIO(yaml_content), conf)

found_issues = []
for p in problems:
    found_issues.append(f"Line {p.line}, Column {p.column}: {p.desc} (Rule: {p.rule})")

if found_issues:
    print("YAML linting issues found:")
    for issue in found_issues:
        print(issue)
else:
    print("No YAML linting issues found.")