Home Azure Cloud Setting Up Python with Azure CLI and SDKs

Setting Up Python with Azure CLI and SDKs

233
0

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):


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.

Previous articleWhy Azure and Python Work So Well Together
Next articleExplaining the Python Program That Connects to Azure via CLI Login
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here