Working with Python and Azure unlocks powerful automation and application development opportunities. By combining the Azure CLI for authentication with the Azure SDKs for Python, you can securely manage resources without storing credentials in your code.
🔹 Prerequisites
Before you begin, make sure you have already followed prerequisites ready (if you already have followed a recipe and did the needful, you can skip it here but read through the prerequisite steps anyways to see if there are any additional things to do):
- Python: If Python is not installed (check with
python --versionon CMD/PowerShell), Install python following the post Getting Started with Python. - Azure CLI installed and logged in as we saw in the note Getting Started with Azure CLI on Your Local Machine, if not already done.
- Create a project folder : Let us call it azure-python-demo. Hint: It is better to organize all project folders in a common folder such as dev.
- Setup a Virtual Environment for Python in that folder and activate it following the study guide: Virtual Environment in Python and How to Set One Up. But do not install any packages such as flask, numpy or pandas.
1. Install Azure SDKs
Inside your virtual environment, install the Python SDKs you need:
pip install azure-identity azure-mgmt-resource
azure-identity→ handles authentication (uses your Azure CLI login under the hood).azure-mgmt-resource→ lets you manage resource groups and deployments.
2. Test with a Python Script
In the azure-python-demo folder, create a file named list_resource_groups.py with below contents but replace the placeholder your-subscription-id with your own actual subscription ID:
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
# Authenticate using your Azure CLI login
credential = DefaultAzureCredential()
subscription_id = "your-subscription-id" # replace with your active subscription
client = ResourceManagementClient(credential, subscription_id)
# List all resource groups
for rg in client.resource_groups.list():
print(rg.name)
Run it:
python list_resource_groups.py
If it prints out all resource groups within the provided subscription, your setup is working 🎉
📝 Important Note: You can find your Subscription ID in the Azure Portal under Subscriptions, or by running the command below (as described in Getting Started with Azure CLI on Your Local Machine):
az account list --output table
This will list all your subscriptions in a nice table format. Copy the SubscriptionId of the one you want to use and paste it into the script.
3. Capture Dependencies
To make your project reproducible, capture your dependencies in a file:
pip freeze > requirements.txt
Later, you (or teammates) can recreate the environment with:
pip install -r requirements.txt
🎯 Next Steps
Now that your Python + Azure setup is ready, you can:
- Work with other services (Storage, Key Vault, Cosmos DB, etc.)
- Automate resource creation and teardown
- Integrate Azure into your applications securely
👉 With CLI-based authentication and Python SDKs, you now have a clean, secure, and extensible environment for building with Azure.