Install & Compatibility
Where this runs
tested against v4.2.0 · 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.484s · 45.2MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 3.9s · import 0.425s · 46MB
44MB installed
● package 44MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
SecretClient
✓ from azure.keyvault.secrets import SecretClient
✗ from azure.keyvault import SecretClient
Clients are modularized into `secrets`, `keys`, and `certificates` sub-packages in Track 2 SDKs.
KeyClient
✓ from azure.keyvault.keys import KeyClient
✗ from azure.keyvault import KeyClient
Clients are modularized into `secrets`, `keys`, and `certificates` sub-packages in Track 2 SDKs.
CertificateClient
✓ from azure.keyvault.certificates import CertificateClient
✗ from azure.keyvault import CertificateClient
Clients are modularized into `secrets`, `keys`, and `certificates` sub-packages in Track 2 SDKs.
DefaultAzureCredential
✓ from azure.identity import DefaultAzureCredential
This quickstart demonstrates how to authenticate with Azure Key Vault using `DefaultAzureCredential` and perform basic secret operations: setting, getting, and deleting a secret. Ensure your environment is configured for Azure authentication and you have sufficient permissions on the Key Vault.
import os
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
# For authentication, ensure you have set up environment variables or Azure CLI login.
# For local development, DefaultAzureCredential will try:
# 1. Environment variables (AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET)
# 2. Managed Identity
# 3. Azure CLI (e.g., `az login`)
# 4. Azure Developer CLI
# 5. Visual Studio Code
# Get your Key Vault URL from environment variable or replace with your actual URL
key_vault_url = os.environ.get("AZURE_KEYVAULT_URL", "https://your-key-vault-name.vault.azure.net/")
if not key_vault_url:
raise ValueError("AZURE_KEYVAULT_URL environment variable or explicit URL is required.")
# Authenticate using DefaultAzureCredential
credential = DefaultAzureCredential()
# Create a SecretClient
secret_client = SecretClient(vault_url=key_vault_url, credential=credential)
secret_name = "MyTestSecret"
secret_value = "HelloFromPythonSDK"
try:
print(f"Setting secret '{secret_name}'...")
set_secret = secret_client.set_secret(secret_name, secret_value)
print(f"Secret set: Name={set_secret.name}, Value={set_secret.value}")
print(f"Getting secret '{secret_name}'...")
retrieved_secret = secret_client.get_secret(secret_name)
print(f"Secret retrieved: Name={retrieved_secret.name}, Value={retrieved_secret.value}")
print(f"Deleting secret '{secret_name}'...")
# Poller for long-running operation, often involved in deletion
poller = secret_client.begin_delete_secret(secret_name)
deleted_secret = poller.result() # Wait for deletion to complete
print(f"Secret deleted: Name={deleted_secret.name}")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure you have set AZURE_KEYVAULT_URL and authenticated (e.g., via `az login`).")
print("Also ensure the authenticated principal has 'Get', 'Set', and 'Delete' secret permissions on the Key Vault.")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'azure.keyvault.secrets'
The specific client library for Key Vault secrets (`azure-keyvault-secrets`) has not been installed, or an outdated import from the deprecated `azure-keyvault` package is being used.
fixInstall the correct package using `pip install azure-keyvault-secrets` and ensure your import statements are updated to `from azure.keyvault.secrets import SecretClient`.
ClientAuthenticationError: DefaultAzureCredential failed to retrieve a token from the included credentials.
The `DefaultAzureCredential` from `azure-identity` is unable to find valid authentication credentials in the current environment, often due to missing environment variables, an expired Azure CLI token, or a misconfigured managed identity.
fixEnsure you are logged into Azure CLI (`az login`), that required environment variables for authentication (e.g., `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_TENANT_ID` for Service Principal) are set, or that the executing Azure resource has a Managed Identity with the appropriate permissions.
(Forbidden)AKV10032: Invalid permission
The authenticated identity (user, service principal, or managed identity) lacks the necessary access policies or Azure RBAC role assignments on the Azure Key Vault to perform the requested operation (e.g., 'Get Secret', 'Set Secret').
fixAssign the correct access policies (e.g., 'Get', 'List', 'Set' under 'Secret Management') to the identity in the Azure Key Vault's Access Policies blade, or assign a suitable Azure RBAC role like 'Key Vault Secrets User'.
AttributeError: 'KeyVaultManagementClient' object has no attribute 'get_secret'
Attempting to perform data plane operations (like getting secrets) using a client from the management plane (`azure.mgmt.keyvault.KeyVaultManagementClient`) instead of the dedicated data plane client.
fixUse `SecretClient` from `azure.keyvault.secrets` for managing secrets, `KeyClient` from `azure.keyvault.keys` for keys, and `CertificateClient` from `azure.keyvault.certificates` for certificates.
Upgrade
Version history
4.2.0latest on PyPI · released Mar 29, 2022
Audit
Dependencies
azure-identityrequiredRequired for authenticating with Azure services, following the standard Azure SDK for Python authentication pattern.