{"id":2410,"library":"bc-python-hcl2","title":"HCL2 Parser","description":"bc-python-hcl2 is a Python library that provides a parser for HashiCorp Configuration Language 2 (HCL2). It is written in Python using Lark and can be used to parse any HCL2 configuration file, such as Terraform files. This package, currently at version 0.4.3, is a fork of the original `python-hcl2` by Amplify and is officially supported by Bridgecrew. The two projects are diverging, so it's important to be aware of which specific package is being used.","status":"active","version":"0.4.3","language":"en","source_language":"en","source_url":"https://github.com/bridgecrewio/python-hcl2","tags":["hcl2","parser","terraform","config","hashicorp","bridgecrew"],"install":[{"cmd":"pip install bc-python-hcl2","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core parsing engine for HCL2 grammar.","package":"lark","optional":false}],"imports":[{"note":"The primary interface is directly through the `hcl2` module, not a submodule of `bc_python_hcl2`.","wrong":"from bc_python_hcl2 import hcl2","symbol":"hcl2","correct":"import hcl2"}],"quickstart":{"code":"import hcl2\nimport os\n\nhcl_content = '''\nvariable \"region\" {\n  description = \"AWS region\"\n  type        = string\n  default     = \"us-east-1\"\n}\n\nresource \"aws_s3_bucket\" \"example\" {\n  bucket = \"my-unique-example-bucket-123\"\n  tags = {\n    Name        = \"My bucket\"\n    Environment = \"Dev\"\n  }\n}\n'''\n\n# Create a dummy HCL file\nwith open('example.tf', 'w') as f:\n    f.write(hcl_content)\n\n# Load HCL from a file\nwith open('example.tf', 'r') as f:\n    parsed_hcl = hcl2.load(f)\n\nprint(parsed_hcl)\n\n# Clean up the dummy file\nos.remove('example.tf')","lang":"python","description":"This quickstart demonstrates how to parse HCL2 content from a file into a Python dictionary using `hcl2.load()`."},"warnings":[{"fix":"Explicitly install `bc-python-hcl2` if you intend to use the Bridgecrew-maintained fork, and consult its specific documentation for API nuances. If your project relies on the Amplify Education version, install `python-hcl2`.","message":"This library (`bc-python-hcl2` by Bridgecrew) is a fork of `python-hcl2` by Amplify Education. While they share a common history, they are now diverging. Ensure you are using the correct package based on your project's needs, as their APIs or behaviors may differ in future versions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that all HCL configuration files you intend to parse adhere strictly to the HCL2 specification. For HCL v1, a different parser would be required.","message":"The library explicitly supports HCL2 only and is not backwards compatible with HCL v1. Attempting to parse HCL v1 syntax will result in errors.","severity":"breaking","affected_versions":"All versions"},{"fix":"Refer to the latest documentation for `hcl2.dumps()`, `hcl2.dump()`, and `hcl2.Builder()` to leverage HCL2 writing capabilities.","message":"Older versions or information might suggest `python-hcl2` is a read-only parser. However, current versions (including `bc-python-hcl2` 0.4.3) support writing/reconstructing HCL2 from Python dictionaries using `hcl2.dumps()`, `hcl2.dump()`, and `hcl2.Builder()`.","severity":"gotcha","affected_versions":"<0.4.0 (for bc-python-hcl2), <6.x (for upstream python-hcl2)"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}