{"id":14840,"library":"python-terraform","title":"Python Terraform","description":"python-terraform is a Python module that provides a wrapper around the Terraform command-line interface (CLI) tool. It allows Python programs to execute Terraform commands (e.g., init, plan, apply, destroy) and capture their output, enabling programmatic interaction with infrastructure as code workflows. The library's latest release, 0.14.0, focuses on compatibility with newer Terraform CLI versions. It follows an irregular release cadence, typically releasing new versions to support significant changes in the Terraform CLI.","status":"active","version":"0.14.0","language":"en","source_language":"en","source_url":"https://github.com/beelit94/python-terraform","tags":["terraform","infrastructure-as-code","iac","cli-wrapper","automation"],"install":[{"cmd":"pip install python-terraform","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"This library wraps the Terraform command-line tool, which must be installed and accessible in the system's PATH.","package":"Terraform CLI","optional":false}],"imports":[{"note":"While 'from python_terraform import *' is often shown in examples, explicit import of 'Terraform' is generally preferred for clarity and avoiding namespace pollution.","symbol":"Terraform","correct":"from python_terraform import Terraform"}],"quickstart":{"code":"import os\nfrom python_terraform import Terraform\n\n# Create a dummy Terraform configuration file for demonstration\n# In a real scenario, this would be an existing main.tf file.\nterraform_dir = \"./my_terraform_project\"\nos.makedirs(terraform_dir, exist_ok=True)\nwith open(os.path.join(terraform_dir, \"main.tf\"), \"w\") as f:\n    f.write('resource \"null_resource\" \"example\" {}')\n\ntf = Terraform(working_dir=terraform_dir)\n\n# Initialize Terraform\nreturn_code, stdout, stderr = tf.init()\nprint(f\"Init Output:\\n{stdout}\\n{stderr}\")\nif return_code != 0:\n    print(\"Terraform Init failed!\")\n    exit(1)\n\n# Plan the changes\nreturn_code, stdout, stderr = tf.plan(capture_output=True, no_color=True)\nprint(f\"Plan Output:\\n{stdout}\\n{stderr}\")\n\n# Apply the changes (skip_plan=True prevents interactive prompt)\nreturn_code, stdout, stderr = tf.apply(skip_plan=True, capture_output=True, no_color=True)\nprint(f\"Apply Output:\\n{stdout}\\n{stderr}\")\n\n# Destroy the infrastructure\nreturn_code, stdout, stderr = tf.destroy(force=True, capture_output=True, no_color=True)\nprint(f\"Destroy Output:\\n{stdout}\\n{stderr}\")\n","lang":"python","description":"This quickstart demonstrates how to initialize, plan, apply, and destroy a simple Terraform configuration using `python-terraform`. It creates a minimal `main.tf` file and then interacts with it programmatically. `capture_output=True` is used to see the real-time output, and `skip_plan=True` and `force=True` are crucial for non-interactive execution of `apply` and `destroy` respectively."},"warnings":[{"fix":"Ensure your `python-terraform` library version is compatible with your installed Terraform CLI version. Upgrade `python-terraform` to 0.14.0 or newer for Terraform CLI versions 0.14+.","message":"Version 0.14.0 of `python-terraform` introduces support for Terraform CLI versions 0.14 and newer. Older versions of `python-terraform` may not be fully compatible with recent Terraform CLI releases, especially concerning changes in output formats or command-line arguments (e.g., changes for Terraform 0.12+).","severity":"breaking","affected_versions":"<0.14.0"},{"fix":"When calling Terraform methods, include `capture_output=True` in the arguments to stream output directly to the console in real-time. Example: `tf.apply(capture_output=True, ...)`.","message":"By default, `python-terraform` captures `stdout` and `stderr` from Terraform commands, which can make long-running operations appear to hang until completion. To see real-time output, you must explicitly pass `capture_output` with any non-`None` value (e.g., `True`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"For `tf.apply()`, use `skip_plan=True`. For `tf.destroy()`, use `force=True`. These flags tell Terraform to proceed without user input. Example: `tf.apply(skip_plan=True)` and `tf.destroy(force=True)`.","message":"Terraform CLI commands like `apply` and `destroy` are interactive by default, requiring user confirmation. Running these commands without suppressing interaction in a script will cause the process to hang.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the Terraform CLI for your operating system and ensure its installation directory is added to your system's PATH. Verify by running `terraform version` in your terminal.","cause":"The Terraform CLI executable is not installed on the system or is not present in the system's PATH environment variable.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'terraform'"},{"fix":"Ensure the `working_dir` points to a valid Terraform root module. Always run `tf.init()` first. Debug the underlying Terraform configuration by manually running `terraform init` and `terraform plan` in the specified directory to identify configuration errors. You can also inspect the `stdout` and `stderr` returned by `tf.init()` for detailed error messages.","cause":"The Terraform working directory specified in `Terraform(working_dir=...)` does not contain valid Terraform configuration files (`.tf`), or `terraform init` was not run successfully, or there's an issue with the Terraform configuration itself (e.g., missing provider definitions, syntax errors).","error":"Terraform Init failed! (or similar errors during plan/apply related to provider plugins)"},{"fix":"Always pass `capture_output=True` to Terraform methods if you need to see real-time output for debugging. Inspect the `stdout` and `stderr` variables returned by the method call for the full error details after execution.","cause":"The `terraform apply` command failed, but because `capture_output=True` was not set, the live error messages were not streamed to the console, making debugging difficult. The underlying Terraform command may have failed due to validation errors, provider issues, or unexpected infrastructure state.","error":"Command 'terraform apply' did not return 0. Real-time output not visible."}],"ecosystem":"pypi"}