Registry / azure / azure-mgmt-policyinsights

azure-mgmt-policyinsights

JSON →
library1.0.1pypypi✓ verified 24d ago

The Azure Management Policy Insights client library for Python (version 1.0.0) provides functionality to query and analyze Azure Policy compliance. It allows retrieving policy states, events, and remediation details to understand resource compliance with defined policies. As part of the broader Azure SDK for Python, it follows a regular release cadence, primarily focusing on stability for 1.x versions and aligning with Azure API updates.

pip install azure-mgmt-policyinsights azure-identity
INSTALL
IMPORT
SIG · AZURE-MGMT-POLICYI
A
azure-mgmt-policyinsights
azurepythonv1.0.1
Install
3.9s avg
Import
572ms
Disk
44MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.596s · 44.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.9s · import 0.548s · 45MB
44MB installed
● package 44MB
Code
Verified usage

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

PolicyInsightsClient
from azure.mgmt.policyinsights import PolicyInsightsClient
DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Standard way to authenticate with Azure services.

Initializes the PolicyInsightsClient using DefaultAzureCredential and attempts to list the first policy event for the given subscription scope. This demonstrates client setup and a basic API interaction.

import os from azure.identity import DefaultAzureCredential from azure.mgmt.policyinsights import PolicyInsightsClient # Get subscription ID from environment variable # Ensure AZURE_SUBSCRIPTION_ID is set, e.g., in your shell or .env file. subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID") if subscription_id == "YOUR_SUBSCRIPTION_ID": print("WARNING: Please set the AZURE_SUBSCRIPTION_ID environment variable for actual API calls.") print("Using a dummy subscription ID for client initialization. API calls will likely fail.") subscription_id = "00000000-0000-0000-0000-000000000000" try: # Authenticate using DefaultAzureCredential # This tries various methods: environment variables, managed identity, Azure CLI, etc. credential = DefaultAzureCredential() # Create a PolicyInsights client client = PolicyInsightsClient(credential, subscription_id) print(f"\nPolicyInsightsClient initialized for subscription: {subscription_id}") # Example: Attempt to list a few policy events # For actual data, you would typically add more filters (e.g., by time, resource). print("\nAttempting to list first few policy events (may require permissions and valid subscription ID)...\n") events_iterator = client.policy_events.list_query_results_for_subscription( scope=f"/subscriptions/{subscription_id}", policy_events_resource="latest", top=1 # Limit to 1 for quickstart output ) found_event = False for event in events_iterator: print(f" Policy Assignment ID: {event.policy_assignment_id}") print(f" Policy Definition ID: {event.policy_definition_id}") print(f" Resource ID: {event.resource_id}") found_event = True break # Only show the first one if not found_event: print(" No policy events found or unable to retrieve. Check your AZURE_SUBSCRIPTION_ID and Azure RBAC permissions.") except Exception as e: print(f"\nAn error occurred during client initialization or API call: {e}") print("Please ensure you have authenticated with Azure (e.g., `az login` or environment variables) and have the necessary permissions (e.g., 'Reader' role).")
Debug
Known issues
gotchaAuthentication and Permissions: Ensure your environment is set up for DefaultAzureCredential (e.g., `az login`, environment variables like AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID) and the authenticated identity has the necessary Azure RBAC permissions (e.g., 'Reader' or 'Policy Insights Data Reader' role at the subscription/resource group scope) to query policy data. Authorization errors (401/403) are common if permissions are missing.
fix
Authenticate via `az login` or configure environment variables for service principal. Assign appropriate RBAC roles via Azure portal or Azure CLI (e.g., `az role assignment create --role "Reader" --scope /subscriptions/<sub-id> --assignee <user-or-sp-id>`).
affects: All
gotchaPolicy Insights vs. Policy Management: This library (`azure-mgmt-policyinsights`) is strictly for *querying* policy states, events, and remediation. It is NOT for *defining* or *assigning* Azure Policies. For policy definition and assignment, use `azure-mgmt-policy`.
fix
Use `azure-mgmt-policy` for creating, updating, or deleting policy definitions, sets, and assignments.
affects: All
gotchaCorrect Scope for Queries: All policy insights queries (e.g., `list_query_results_for_subscription`, `list_query_results_for_resource_group`) require a correctly formatted 'scope' parameter. Common mistakes include malformed scope strings or attempting to query a scope for which the authenticated identity lacks permissions.
fix
Ensure the `scope` parameter follows Azure Resource Manager ID format (e.g., `/subscriptions/{subscription_id}`, `/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}`). Verify permissions for the specified scope.
affects: All
breakingThe `azure-mgmt-policyinsights` library, or one of its dependencies, requires the `six` compatibility library. A `ModuleNotFoundError` indicates that `six` was not found, preventing the library from being imported and used.
fix
Ensure `six` is installed in your environment. You can explicitly install it by running `pip install six`. While `six` is typically installed automatically as a dependency of `azure-mgmt-policyinsights`, it might be missing in some specific environments or installation setups.
affects: All
gotchaMissing Dependency: The `azure-mgmt-policyinsights` library requires the `six` package. This error occurs if `six` is not installed in your Python environment, preventing the library from being imported.
fix
Install the missing `six` package using pip: `pip install six`. Ensure all required dependencies are installed, especially in minimal or custom environments.
affects: All
Upgrade
Version history
1.0.1latest on PyPI · released May 19, 2026
Audit
Dependencies
azure-identityoptionalCommonly used for authenticating with Azure services.
msrestrequiredCore dependency for REST client operations.
msrestazurerequiredCore dependency for Azure-specific REST client operations.
Agent activity
61 hits · last 30 days
node
52
OpenAI (training)
2
Resources