Introduction

This article demonstrates how to set up a Windows development system with Python and the Azure SDK for Python.

After my article, Azure – Lock a VM to Prevent Deletion, I wrote the Azure CLI equivalent commands in Python. On the surface that appears to be an easy task. In reality, because of different Azure SDK for Python module versions, I spent hours hunting down strange problems. Microsoft has deprecated some modules, and because those modules were installed on my system, I had these problems. Therefore, I decided to document the procedure to set up a Windows system for Python, PHP, Node.js, Go, and .NET. This article only covers Python. This article does not cover Visual Studio Code, as I use Visual Studio Enterprise which is an entirely different platform for development.

This article includes several example programs that I wrote using the Azure Resource Manager – Management Locks SDK. I had too many unanswered details after writing the article on locking VMs. Knowing how to use the CLI is important, but sometimes the real knowledge comes from using the API or SDK and deploying for different scenarios. To understand the context of the example programs, read these articles that I wrote:

Step 1 – Create a Hyper-V Virtual Machine

This step is optional. For this article, I wanted a clean environment with no remnants of other languages, libraries, or programs.

Configuration:

  • Windows 10 Enterprise – Download ISO Image Link
  • Memory: 4 GB
  • Disk: 127 GB – Dynamically Expanding
  • Generation 2 – UEFI, 64-bit OS
  • Run Windows Update. Repeat until there is no more update available.
  • Create a directory to store the downloads for the next steps. I use C:\Downloads. That makes it easy to delete the downloads, archive, them, etc.
  • Reboot the system one last time.

Step 2 – Install Tools

Select from this list and install your favorite tools:

Step 3 – Install the Azure CLI

The Azure CLI is not required but is very useful. During development, I use the Python class DefaultAzureCredential to authorize my program from the CLI’s authorization (credentials).

Install the Azure CLI from this link:

Install Azure CLI on Windows

Authorize the Azure CLI

  • az login

An alternative is to use a service principal and I will cover that in the next article.

Step 4 – Install Python 3.9

The current version of Python is 3.9.7 released 2021-08-30. I could not find Microsoft comments for version limitations for Python 3.x, so I will install the latest GA version.

  • Go to the Python download page. [link]
  • Download the latest version for your system. The Python site detects your operating system.

Installation Tips:

  • Enable Add Python 3.9 to PATH.
  • Select Customize installation.
  • Enter a short path such as C:\Python39 for Customize install location. This is to prevent long path name problems later.

Step 5 – Create a Python Projects Working Directory

I do not use my home directory for Python projects. Instead, I create a working directory, a project name, a subproject name, and later directories for each program name. Example c:\work\Azure\Python\ResourceManager\ManagementLocks\List-Locks. This makes backups easier to specify and also makes Git Repositories easier to set up.

Step 6 – Create your first Azure SDK Based Program

I will go into detail in Part 2, but this will help you get started quickly.

Create the program directory

Based on the working directory, create a directory named “List-Locks” or similar.

Example:

  • cd c:\work\Azure\Python\ResourceManager\ManagementLocks
  • mkdir List-Locks
  • cd List-Locks

Enable the Python Virtual Environment

The following command will create a directory name env to store the Python virtual environment, create the environment, and then activate the environment. This is a very important step. There are so many collisions and conflicts with the Azure SDK for Python. This step isolates the environment and installs packages to the program’s working directory.

  •  python -m venv env
  •  env\Scripts\activate

Update the Python PIP program

  • python -m pip install -U pip

Create a requirements.txt file

The file requirements.txt declares the Python packages that are required for a program to operate correctly.

Using your favorite editor, create a file named requirments.txt with the following content:

Install dependencies

  • pip install -r requirements.txt

Create the program file

Create a file named list_locks.py

Setup environment variable

This step requires the Azure CLI to display the account Subscription ID. This is also available in the Azure Portal.

Run the following command:

  • az account show

The account information is displayed in JSON. Copy the value for “id” without the quotes.

Run the following command to set the environment variable AZURE_SUBSCRIPTION_ID. Replace the text REPLACE_ME with the id from the previous command output.

  • set AZURE_SUBSCRIPTION_ID=REPLACE_ME

Run the program

If there are any Management Locks within the subscription, a formatted table will be displayed.

Run the Python program:

  • python list_locks.py

Example output:

Next Steps

In the next article, I will go into detail about the Azure Resource Manager SDK, Authorization, and how to interface with Azure.

Photography Credits

I write free articles about technology. Recently, I learned about Pexels.com which provides free images. The image in this article is courtesy of Pixabay at Pexels.