skillfed

python-hcl2

A parser for HCL2

python-hcl2 v8.1.2 6.3M downloads/30d#1,929 on PyPI316
Permissive license MIT Active released

What it is and what it does

python-hcl2 is a parser that reads HCL2 configuration syntax—the language used by Terraform and other HashiCorp tools—and converts it into Python dictionaries, or vice versa. It uses the Lark parsing library and the regex module as its core dependencies. The package supports HCL2 only (not the older HCL v1) and works with any HCL2 config file, including Terraform configurations.

Beyond basic parsing, the package provides a Builder API for constructing HCL2 documents programmatically from Python, and ships three CLI tools: hcl2tojson and jsontohcl2 for format conversion, and hq—a jq-like query tool for extracting and inspecting parts of HCL2 files. It is actively maintained, supports Python 3.8 through 3.13, and carries no known security vulnerabilities.

Use it for:

  • Convert Terraform .tf files to JSON for programmatic inspection or transformation in a Python application.
  • Build HCL2 configuration files dynamically from Python code without hand-writing HCL syntax.
  • Query specific values from HCL2 files using the hq CLI tool for compliance checks or configuration audits.
  • Integrate HCL2 parsing into infrastructure-as-code tooling or configuration management scripts.
  • Round-trip HCL2 ↔ JSON for data pipeline or API integration workflows.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Parses HCL2 configuration files (used by Terraform and other HashiCorp tools) into Python data structures, and converts Python dicts back to HCL2 format. Includes CLI tools for format conversion and querying.

Yes. The package is actively maintained, has no known vulnerabilities, installs with minimal friction (two lightweight dependencies), supports current Python versions, and solves a real problem for anyone working with Terraform or other HCL2-based tools. The permissive MIT license poses no barrier. Install it if you need to parse, query, or generate HCL2 files from Python.

Install

python-hcl2 on PyPI

pip

pip install python-hcl2

uv

uv add python-hcl2

poetry

poetry add python-hcl2

Installing python-hcl2

Before you install

Low friction: pure Python wheel with only two runtime dependencies (lark and regex). Actively maintained with a recent release; last commit was 2026-07-18 and the project is not archived.

License in practice

MIT license (permissive) means you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.

Quickstart

pip install python-hcl2

import hcl2

with open("main.tf") as f:
    data = hcl2.load(f)

hcl_string = hcl2.dumps(data)

Requires Python 3.8 or higher.

Verify before relying

  • Whether the package handles all HCL2 syntax edge cases or has known parsing limitations beyond those documented.
  • Performance characteristics when parsing very large HCL2 files or directories.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.8.0)
Install friction low — pure-Python wheel
Runtime dependencies 2 — lark, regex
Maintenance actively maintained — 126 days since the last release
Last repo commit
First released
Downloads 6,339,437/month — #1,929 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: python_hcl2-8.1.2-py3-none-any.whl

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Topic :: Software Development :: Libraries :: Python Modules

Tags

HCL2 parser PythonTerraform config parserHCL to JSON converterparse HCL2 filesHCL2 query toolinfrastructure as code parser
terraformconfiguration-parsinginfrastructure-as-code

More Python Modules packages