This post will share This post is continuing from Part 1. And I will be showing out how to include Azure SDK libraries/packages to install, authentication and setting up a python virtual environment in VS Code.
The bare essentials for Azure Python packages are:
pip install azure-identity #for authentication
pip azure-mgmt-resource # resource management client library for managing azure resources
To authenticate to an Azure tenant, some of the options is to use your user account or a non-human account called the Azure Service Principal. The service principle is the best practice for running code in the context CI/CD pipelines, scheduled jobs or other automation services.
But for simplicity, I’ll demonstrate with a user’s account by calling
credential = AzureCliCredential()
By running this you would be prompted with a pop up to login with email and password and any MFA.
Then identify the type of resources such as storage account, key vault, app service that you want to work with. You can look them up at
https://azure.github.io/azure-sdk/releases/latest/all/python.html
For storage related, you can search as follows:

And to install run pip install azure-mgmt-storage or include azure-storage-blog in requirements.txt
Note that Azure SDK for Python have two types of libraries – client and management
Azure clients are designed for interacting with and consuming Azure services. They provide a high-level, idiomatic, and user-friendly interface for developers to work with specific Azure services. These libraries are intended for use by application developers who want to integrate their applications with Azure services.
For example

pip install azure-storage-blob
In contrast, Azure management libraries are designed for managing and provisioning Azure resources. They perform operations related to resource deployment, management, and governance. These libraries are intended for use by IT administrators and DevOps engineers.
For example
from azure.mgmt.storage import
storage_client = StorageManagementClient(credential, subscription_id)
StorageManagementClient<br>storage_client.storage_accounts.begin_create(RESOURCE_GROUP_NAME, STORAGE_ACCOUNT_NAME,
{
"location" : LOCATION,
"kind": "StorageV2",
"sku": {"name": "Standard_LRS"}
}
Python Virtual Environment
To help manage your python development environment setup is to consider setting a python “virtual environment”. It is a self-contained directory that contains a Python interpreter and a set of standard libraries. It allows you to create an isolated environment for your Python projects, where you can install and manage dependencies independently of the system-wide Python installation. This isolation helps avoid conflicts between different projects and ensures that each project has its own set of dependencies.
Here’s how I create a virtual environment in my vscode.
In VSCode, type CTRL+Shift+P and enter and select

Select venv

Select Python version. This allows for that flexibility and isolation to work on the specific versions of software, libraries and dependencies.

Since I have an existing requirements.txt, it will install Python packages based on what was defined in this requirements.txt file.

A .venv folder will be created and in it will be environment specific config and python packages.

To activate or work within the virtual environment run in command prompt:

I can choose to pip install additional packages for this venv. In the Python vscode extension, you can view the venv.

The working and python files are still in my main repo folder and I do not put them in the .venv folder.
You can come out of the .venv by running deactivate

In the next post Part 3, I will go through a detailed walk through and show the VSCode experience.
References:
Pingback: Step-by-Step Azure SDK for Python in Windows VS Code – Part 3 – Roy Kim on Azure and Microsoft 365
Pingback: Step-by-Step Using Azure SDK for Python in Windows VS Code – Part 1 – Roy Kim on Azure and Microsoft 365