title | description | ms.date | ms.topic | ms.custom |
---|---|---|---|---|
List resource groups and resources using the Azure libraries for Python |
Use the resource management library in the Azure SDK for Python to list resource groups and resources in a group. |
04/23/2025 |
conceptual |
devx-track-python, py-fresh-zinc |
This example demonstrates how to use the Azure SDK management libraries in a Python script to perform two tasks:
- List all the resource groups in an Azure subscription.
- List resources within a specific resource group.
All the commands in this article work the same in Linux/macOS bash and Windows command shells unless noted.
The Equivalent Azure CLI commands are listed later in this article.
If you haven't already, set up an environment where you can run this code. Here are some options:
- Configure a Python virtual environment using
venv
or your tool of choice. To start using the virtual environment, be sure to activate it. To install python, see Install Python.
#!/bin/bash
# Create a virtual environment
python -m venv .venv
# Activate the virtual environment
source .venv/Scripts/activate # only required for Windows (Git Bash)
# Create a virtual environment
python -m venv venv
# Activate the virtual environment
. .\venv\Scripts\Activate.ps1
-
Use a conda environment. To install Conda, see Install Miniconda.
-
Use a Dev Container in Visual Studio Code or GitHub Codespaces.
Create a file named requirements.txt with the following contents:
:::code language="txt" source="~/../python-sdk-docs-examples/resource_group/requirements.txt":::
In a terminal or command prompt with the virtual environment activated, install the requirements:
pip install -r requirements.txt
Create a Python file named list_groups.py with the following code. The comments explain the details:
:::code language="python" source="~/../python-sdk-docs-examples/resource_group/list_groups.py":::
Create a Python file named list_resources.py with the following code. The comments explain the details.
By default, the code lists resources in "myResourceGroup". To use a different resource group, set the RESOURCE_GROUP_NAME
environment variable to the desired group name.
:::code language="python" source="~/../python-sdk-docs-examples/resource_group/list_resources.py":::
Later in this article, you sign in to Azure with the Azure CLI to run the sample code. If your account has permissions to create and list resource groups in your Azure subscription, the code will run successfully.
To use such code in a production script, you can set environment variables to use a service principal-based method for authentication. To learn more, see How to authenticate Python apps with Azure services. You need to ensure that the service principal has sufficient permissions to create and list resource groups in your subscription by assigning it an appropriate role in Azure; for example, the Contributor role on your subscription.
-
If you haven't already, sign in to Azure using the Azure CLI:
az login
-
Set the
AZURE_SUBSCRIPTION_ID
environment variable to your subscription ID. (You can run the az account show command and get your subscription ID from theid
property in the output):export AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
$env:AZURE_SUBSCRIPTION_ID="00000000-0000-0000-0000-000000000000"
-
List all resources groups in the subscription:
python list_groups.py
-
List all resources in a resource group:
python list_resources.py
By default, the code lists resources in "myResourceGroup". To use a different resource group, set the
RESOURCE_GROUP_NAME
environment variable to the desired group name.
The following Azure CLI command lists resource groups in a subscription:
az group list
The following command lists resources within the "myResourceGroup" in the centralus region (the location
argument is necessary to identify a specific data center):
az resource list --resource-group myResourceGroup --location centralus
- Example: Provision a resource group
- Example: Provision Azure Storage
- Example: Use Azure Storage
- Example: Provision a web app and deploy code
- Example: Provision and query a database
- Example: Provision a virtual machine
- Use Azure Managed Disks with virtual machines
- Complete a short survey about the Azure SDK for Python