Registry / devops / pyhcl
library0.4.5pypypi✓ verified 27d ago

pyhcl is a Python library for parsing HCL (HashiCorp Configuration Language) files and strings into Python dictionaries. It aims to be compatible with HCL versions commonly used by tools like Terraform. The library is currently at version 0.4.5 and is actively maintained with bug fixes and compatibility updates, though releases are irregular.

pip install pyhcl
INSTALL
IMPORT
SIG · PYHCL
P
pyhcl
devopspythonv0.4.5
Install
1.6s avg
Import
43ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.5 · 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.044s · 18.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.042s · 19MB
16MB installed
● package 16MB
Code
Verified usage

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

hcl
import hcl

Parses an HCL string into a Python dictionary. The `hcl.loads()` function handles string input, while `hcl.load()` is used for file-like objects.

import hcl hcl_string = ''' variable "region" { description = "AWS region" type = string default = "us-east-1" } resource "aws_instance" "web" { ami = "ami-0abcdef1234567890" instance_type = "t2.micro" tags = { Name = "HelloWorld" } } ''' parsed_data = hcl.loads(hcl_string) print(parsed_data.get('resource')) print(parsed_data.get('variable'))
Debug
Known issues
breakingpyhcl dropped support for Python 2 in version 0.3.0. Users on older Python 2 environments must upgrade their Python version or use pyhcl < 0.3.0.
fix
Upgrade Python to 3.6+ or pin pyhcl to a version prior to 0.3.0 (e.g., `pyhcl==0.2.0`).
affects: <0.3.0
breakingThe return type of `hcl.load` and `hcl.loads` changed significantly in version 0.2.0. Previously, it returned a custom `hcl.parser.HCL` object; now, it returns a standard Python `dict`. Code expecting the old object structure will break.
fix
Update parsing logic to expect and handle a standard Python dictionary. Access data using dictionary methods like `.get()` or `[]`.
affects: <0.2.0
gotchaThe primary module for importing is `hcl`, not `pyhcl`. Developers sometimes try `from pyhcl import hcl` or `from pyhcl import load`, which will result in an `ImportError`.
fix
Always use `import hcl` to access the parsing functions.
affects: All versions
gotchaWhile `pyhcl` aims for broad HCL compatibility, it may not perfectly support every nuance or the very latest syntax of the evolving HCL specification, especially complex expressions or new language features introduced in newer HCL versions.
fix
For critical or highly complex HCL parsing, validate `pyhcl`'s output against the official HCL parser or actual tool behavior. Consider simpler HCL structures if encountering parsing issues.
affects: All versions
Errors
Common errors & fixes
ValueError: Line X, column Y: unexpected COMMA
PyHCL, especially older versions or with certain HCL configurations, does not correctly handle trailing commas in lists or maps, which are valid in HCL and often enforced by formatters.
fix
Remove trailing commas from lists and maps in your HCL input. For example, change `["a", "b",]` to `["a", "b"]`.
ModuleNotFoundError: No module named 'ply'
The 'ply' (Python Lex-Yacc) library, a dependency of `pyhcl`, is not installed or not correctly found by the Python environment during `pyhcl` installation or import.
fix
Install `ply` explicitly before or alongside `pyhcl` using `pip install ply pyhcl` or by installing `ply` first (`pip install ply`) and then `pyhcl` (`pip install pyhcl`).
ValueError: Empty string
`pyhcl.loads()` (or `pyhcl.load()` for an empty file) raises a ValueError when attempting to parse an empty string or an empty HCL file, as it expects valid HCL content.
fix
Ensure that the string or file passed to `pyhcl.loads()` or `pyhcl.load()` is not empty and contains valid HCL syntax. Implement a check for empty input before parsing.
ValueError: Line X, column Y: unexpected TOKEN (when parsing HCL2 syntax)
`pyhcl` does not fully support HCL2 syntax, which is used by modern Terraform versions (e.g., Terraform 0.12 and newer). Attempting to parse HCL2 files with `pyhcl` will result in various syntax errors.
fix
For HCL2 files, use a library specifically designed for HCL2, such as `python-hcl2`. Install it with `pip install python-hcl2` and then use `import hcl2`.
Upgrade
Version history
0.4.5latest on PyPI · released Sep 1, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
pyhcl — pip install pyhcl · libregistry