Step-by-Step Using Azure SDK for Python in Windows VS Code – Part 1

This is a beginners guide on starting from scratch setting up a simple development environment and run some Python code to create and manage azure resources.

Install Python

https://wiki.python.org/moin/BeginnersGuide/Download

For windows download go to https://www.python.org/downloads/

Install VS Code to develop and run python code.

For windows download go to https://www.python.org/downloads/

Install Python extension for Visual Studio Code which will help run, debug and provide intellisense support. This extension also installs another VS Code extension called Pylance.
Pylance provides some awesome features for Python 3 including Signature help, with type information, parameter suggestions, code completion, etc.

Once these extensions are installed, creating a .py file is recognized and can be executed by the Python Interpreter. The top right has the run button to execute your code.

Here is the debugging tool in action where you can place breakpoints and see variable values.

Intellisense support.

Install Azure SDK for Python Libraries by using Python pip command for installing and managing Python packages. This file that contains a list of packages needed for your code. It is a central place and can be stored in your repo.

In your project folder, create requirements.txt and add the list of packages you want. The fundamental packages are azure-mgmt-resource, azure-identity, azure-mgmt-subscription.

To install what is in the requirements.txt,
pip install -r requirements.txt

To see what is installed, run

pip list

To uninstall,

pip uninstall -r requirements.txt

Setup environment variables such as tenant id and subscription id or any other you desire. Since I am sharing this code in my public github repos, I like to reference these ids as environment variables as a good practice.

In Python, code, you can read an environment variable with the following and importing the os package

You can set the environment in a windows command prompt by running the set command and run set | more to page through the list of environment variables.

So it is a good to learn to reference and manage env variables for API keys, GUIDS, secrets instead of hardcoding in your code.

In VS Code, it is good to be aware which Python Interpreter is selected. You can check and change here.

In the VS Code Python extension, you can see the Python versions installed and respective packages. You can install, update and delete packages in this UI.

In the next post Part 2, I’ll show some further developer setup in vscode and python packages for Azure.

References:

One thought on “Step-by-Step Using Azure SDK for Python in Windows VS Code – Part 1

  1. Pingback: Step-by-Step Using Azure SDK for Python in Windows VS Code – Part 2 – Roy Kim on Azure and Microsoft 365

Leave a Reply