Registry / azure / az.cli

az.cli

JSON →
library0.5pypypi✓ verified 25d ago

az.cli provides a simple Python interface to execute Azure CLI commands directly from Python scripts. It leverages the subprocess module to call the `az` executable installed on the system, passing commands as strings and returning exit codes, parsed JSON results, and raw logs. The current version is 0.5, with releases occurring infrequently as new features or critical bug fixes are implemented.

pip install az.cli==0.5
INSTALL
IMPORT
SIG · AZ.CLI
A
az.cli
azurepythonv0.5
Install
21.4s avg
Import
Disk
662MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.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.1s · import 0.000s · 586.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 42.6s · import 0.000s · 588MB
662MB installed
● package 662MB
Code
Verified usage

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

az
import az
from az.cli import az

This quickstart demonstrates how to import and use the `az` function to execute an Azure CLI command. It's critical to check the `exit_code` for command success and to understand that `result_dict` only contains parsed JSON output. For other formats or errors, the raw output is in `logs`. Ensure your Azure CLI is installed and you are logged in for commands like `group list` to work.

from az.cli import az # Execute an Azure CLI command # For example, to list resource groups. Make sure you are authenticated with Azure CLI (e.g., 'az login'). exit_code, result_dict, logs = az("group list") if exit_code == 0: print("Azure CLI command executed successfully!") print("Parsed Result (if JSON output):", result_dict) if result_dict and isinstance(result_dict, list) and len(result_dict) > 0: print(f"First resource group name: {result_dict[0].get('name', 'N/A')}") else: print(f"Azure CLI command failed with exit code: {exit_code}") print("Error logs:", logs) # Example of a command that might not return JSON or returns raw string output # exit_code, result_dict, logs = az("account show --output tsv") # if exit_code == 0: # print("TSV Output (result_dict will be empty for non-JSON):") # print(logs) # Raw output will be in 'logs' string
az --version
Debug
Known issues
gotchaThe `az.cli` Python library is merely a wrapper. It does NOT bundle the Azure CLI executable. You MUST have the Azure CLI installed and authenticated on the host system where your Python script runs for `az.cli` to function correctly.
fix
Install the Azure CLI via its official documentation (e.g., apt, yum, brew, PowerShell, MSI installer) and ensure it's in your system's PATH. Then, run `az login` to authenticate.
affects: All versions
gotchaError handling for Azure CLI commands through `az.cli` relies on checking the `exit_code` and `logs` returned by the `az()` function. Failures in the underlying CLI command do not raise Python exceptions directly but are indicated by a non-zero `exit_code` and detailed messages in `logs`.
fix
Always unpack the return value (e.g., `exit_code, result_dict, logs = az("command")`) and explicitly check `if exit_code != 0:` to handle command failures and retrieve error messages from `logs`.
affects: All versions
gotchaThe `result_dict` returned by `az()` is populated only when the underlying Azure CLI command successfully outputs valid JSON. For commands that produce other output formats (e.g., `--output tsv`, `--output table`) or on command failure, `result_dict` will be empty or `None`. In such cases, the raw output will be available in the `logs` string.
fix
To utilize `result_dict`, ensure your Azure CLI commands explicitly request JSON output (e.g., `az account show --output json`). For other formats, process the `logs` string directly.
affects: All versions
deprecatedStarting with version 0.4, the `requirements.txt` file was removed to promote a 'loosely coupled' approach. This means the library no longer declares or enforces its direct Python package dependencies (e.g., for JSON parsing).
fix
While `az.cli` itself has minimal Python dependencies, users should ensure their environment has any necessary basic packages (like a JSON parser, though Python's `json` module is standard) if issues arise with `result_dict` processing.
affects: >=0.4
Upgrade
Version history
0.5latest on PyPI · released Mar 14, 2021
Audit
Dependencies
Azure CLI (external executable)requiredThis library acts as a wrapper around the Azure CLI executable. It must be installed, configured, and authenticated separately on the host system where the Python script runs.
Agent activity
38 hits · last 30 days
node
32
OpenAI (training)
1
Resources
az.cli — pip install az.cli · libregistry