Registry / devops / python-hcl2

python-hcl2

JSON →
library8.1.3pypypi✓ verified 27d ago

python-hcl2 is a robust parser for HCL2 (HashiCorp Configuration Language v2), primarily used for Terraform configuration files. It converts HCL2 code into Python data structures (lists of dictionaries) and supports reverse transformation from Python dicts back to HCL2. The current version is 8.1.1, with a release cadence that has seen frequent updates, especially around the major v8.0.0 overhaul.

pip install python-hcl2
INSTALL
IMPORT
SIG · PYTHON-HCL2
P
python-hcl2
devopspythonv8.1.3
Install
2.4s avg
Import
254ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v8.1.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.270s · 22.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.238s · 23MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

load
from hcl2 import load
import hcl2
loads
from hcl2 import loads
import hcl2
parse
from hcl2 import parse
import hcl2

This quickstart demonstrates parsing an HCL2 configuration string using `hcl2.loads()` and then serializing the resulting Python list of dictionaries back into an HCL2 string using `hcl2.dumps()`. It highlights the v8 breaking change where parsing now returns a list of dictionaries.

import hcl2 import json hcl2_config = ''' resource "aws_s3_bucket" "example" { bucket = "my-unique-bucket-name" acl = "private" tags = { Environment = "Development" Project = "MyApp" } } variable "region" { description = "AWS region" type = string default = "us-east-1" } ''' # Parse the HCL2 string parsed_data = hcl2.loads(hcl2_config) # Print the parsed data (v8 returns a list of dictionaries) print("--- Parsed Data (Python List of Dicts) ---") print(json.dumps(parsed_data, indent=2)) # Accessing specific blocks (example: the first resource block) if parsed_data and 'resource' in parsed_data[0]: resource_block = parsed_data[0]['resource']['aws_s3_bucket']['example'] print("\n--- Extracted Resource Block ---") print(json.dumps(resource_block, indent=2)) # Serialize back to HCL2 (v8 feature) recreated_hcl2 = hcl2.dumps(parsed_data) print("\n--- Recreated HCL2 ---") print(recreated_hcl2)
Debug
Known issues
breakingStarting with version 8.0.0, the `hcl2.load()` and `hcl2.loads()` functions now return a list of dictionaries instead of a single dictionary. This change better represents HCL2's top-level block structure.
fix
Update your code to iterate over the returned list or access elements by index. For example, `hcl2.loads(s)[0]` if you expect a single top-level block, or iterate `for block in hcl2.loads(s): ...`.
affects: >=8.0.0
gotchaWhile `python-hcl2` offers robust parsing, HCL2's dynamic features like interpolation (`${...}`), template directives (`%{if ...}`), and complex expressions can still lead to parsing nuances. Edge cases may not always be perfectly represented as standard Python data structures or round-trip identically.
fix
Consult the 'Limitations' section in the official documentation for known quirks. Thoroughly test parsing and serialization with your specific HCL2 configurations, especially those with advanced interpolation or conditional logic, to ensure expected behavior.
affects: All versions
gotchaRound-tripping (parsing HCL2 to Python then serializing back to HCL2) might not preserve original formatting, comments, or all subtle syntactic nuances present in the source HCL2 file. While v8 significantly improved bidirectional conversion, a perfect byte-for-byte fidelity is not guaranteed.
fix
If exact HCL2 formatting preservation is critical (e.g., for version control systems comparing files), consider using specialized HCL2 formatting tools externally or validate the output of `hcl2.dumps()` carefully. For programmatic manipulation where logical correctness is paramount, `hcl2.dumps()` is generally reliable.
affects: All versions, particularly relevant for >=8.0.0 when using `dumps()`
Upgrade
Version history
8.1.3latest on PyPI · released Aug 26, 2026
Audit
Dependencies
larkrequiredCore parsing engine, providing grammar and lexing capabilities for HCL2. Required for all parsing operations.
Agent activity
23 hits · last 30 days
node
22
Resources
python-hcl2 — pip install python-hcl2 · libregistry