python-hcl2
A parser for HCL2
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-hcl2uv
uv add python-hcl2poetry
poetry add python-hcl2Installing 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
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
bc-python-hcl2Parses HCL2 configuration files (such as…
permissive · top 5,000 on PyPI
pyhclParses HCL (HashiCorp Configuration Language)…
copyleft · top 5,000 on PyPI
tfparsetfparse parses and evaluates Terraform HCL…
permissive · top 15,000 on PyPI
tree-sitter-hclProvides a tree-sitter parser grammar for HCL…
permissive · top 15,000 on PyPI
fhconfparserReads configuration files in INI, TOML, or JSON…
permissive · top 15,000 on PyPI
cdktfDefines cloud infrastructure using Python code…
copyleft · top 15,000 on PyPI
lark-jsLark-js is a command-line tool that generates…
permissive · top 15,000 on PyPI
python-terraformWraps the Terraform command-line tool, allowing…
permissive · top 15,000 on PyPI
lark-parserLark is a parsing library that builds abstract…
permissive · top 5,000 on PyPI
hvachvac is a Python client library for HashiCorp…
permissive · top 1,000 on PyPI