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.
Install & Compatibility
Where this runs
tested against v0.10.1 · 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
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.044s · 19.2MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 2.4s · import 0.040s · 20MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
While 'from python_terraform import *' is often shown in examples, explicit import of 'Terraform' is generally preferred for clarity and avoiding namespace pollution.
from python_terraform import Terraform
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.
import os
from python_terraform import Terraform
# Create a dummy Terraform configuration file for demonstration
# In a real scenario, this would be an existing main.tf file.
terraform_dir = "./my_terraform_project"
os.makedirs(terraform_dir, exist_ok=True)
with open(os.path.join(terraform_dir, "main.tf"), "w") as f:
f.write('resource "null_resource" "example" {}')
tf = Terraform(working_dir=terraform_dir)
# Initialize Terraform
return_code, stdout, stderr = tf.init()
print(f"Init Output:\n{stdout}\n{stderr}")
if return_code != 0:
print("Terraform Init failed!")
exit(1)
# Plan the changes
return_code, stdout, stderr = tf.plan(capture_output=True, no_color=True)
print(f"Plan Output:\n{stdout}\n{stderr}")
# Apply the changes (skip_plan=True prevents interactive prompt)
return_code, stdout, stderr = tf.apply(skip_plan=True, capture_output=True, no_color=True)
print(f"Apply Output:\n{stdout}\n{stderr}")
# Destroy the infrastructure
return_code, stdout, stderr = tf.destroy(force=True, capture_output=True, no_color=True)
print(f"Destroy Output:\n{stdout}\n{stderr}")
terraform --version
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.
Resources
No resource links recorded.