Configure your local Python dev environment for Azure

To develop Python applications using Azure, you first want to configure your local development environment. Configuration includes creating an Azure account, installing tools for Azure development, and connecting those tools to your Azure account.

Developing on Azure requires Python 3.7 or higher. To verify the version of Python on your workstation, in a console window type the command python3 --version for macOS/Linux or py --version for Windows.

Create an Azure Account

To develop Python applications with Azure, you need an Azure account. Your Azure account is the credentials you use to sign-in to Azure with and what you use to create Azure resources.

If you're using Azure at work, talk to your company's cloud administrator to get your credentials used to sign-in to Azure.

Otherwise, you can create an Azure account for free and receive 12 months of popular services for free and a $200 credit to explore Azure for 30 days.

Use the Azure portal

Once you have your credentials, you can sign in to the Azure portal at https://portal.azure.com. The Azure portal is typically easiest way to get started with Azure, especially if you're new to Azure and cloud development. In the Azure portal, you can do various management tasks such as creating and deleting resources.

If you're already experienced with Azure and cloud development, you'll probably start off using tools as well such as Visual Studio Code and Azure CLI. Articles in the Python developer center show how to work with the Azure portal, Visual Studio Code, and Azure CLI.

Use Visual Studio Code

You can use any editor or IDE to write Python code when developing for Azure. However, you may want to consider using Visual Studio Code for Azure and Python development. Visual Studio Code provides many extensions and customizations for Azure and Python, which make your development cycle and the deployment from a local environment to Azure easier.

For Python development using Visual Studio Code, install:

  • Python extension. This extension includes IntelliSense (Pylance), Linting, Debugging (multi-threaded, remote), Jupyter Notebooks, code formatting, refactoring, unit tests, and more.

  • Azure Tools extension pack. The extension pack contains extensions for working with Azure App Service, Azure Functions, Azure Storage, Azure Cosmos DB, and Azure Virtual Machines in one convenient package. The Azure extensions make it easy to discover and interact with the Azure.

To install extensions from Visual Studio Code:

  1. Press Ctrl+Shift+X to open the Extensions window.
  2. Search for the Azure Tools extension.
  3. Select the Install button.

Screenshot of the Visual Studio Code showing extensions panel searching for the Azure Tools extension pack.

To learn more about installing extensions in Visual Studio Code, refer to the Extension Marketplace document on the Visual Studio Code website.

After installing the Azure Tools extension, sign in with your Azure account. On the left-hand panel, you'll see an Azure icon. Select this icon, and a control panel for Azure services will appear. Choose Sign in to Azure... to complete the authentication process.

Screenshot of the Visual Studio Code showing how to sign-in the Azure tools to Azure.

Note

If you see the error "Cannot find subscription with name [subscription ID]", this may be because you are behind a proxy and unable to reach the Azure API. Configure HTTP_PROXY and HTTPS_PROXY environment variables with your proxy information in your terminal:

# Windows
set HTTPS_PROXY=https://username:password@proxy:8080
set HTTP_PROXY=http://username:password@proxy:8080
# macOS/Linux
export HTTPS_PROXY=https://username:password@proxy:8080
export HTTP_PROXY=http://username:password@proxy:8080

Use the Azure CLI

In addition to the Azure portal and Visual Studio Code, Azure also offers the Azure CLI command-line tool to create and manage Azure resources. The Azure CLI offers the benefits of efficiency, repeatability, and the ability to script recurring tasks. In practice, most developers use both the Azure portal and the Azure CLI.

The Azure CLI is installed through homebrew on macOS. If you don't have homebrew available on your system, install homebrew before continuing.

brew update && brew install azure-cli

This command will first update your brew repository information and then install the Azure CLI.

After installing, sign-in to your Azure account from the Azure CLI by typing the command az login in a terminal window on your workstation.

az login

The Azure CLI will open your default browser to complete the sign-in process.

Configure Python virtual environment

When creating Python applications for Azure, it's recommended to create a virtual environment for each application. A virtual environment is a self-contained directory for a particular version of Python plus the other packages needed for that application.

To create a virtual environment, follow these steps.

  1. Open a terminal or command prompt.

  2. Create a folder for your project.

  3. Create the virtual environment:

    # py -3 uses the global python interpreter. You can also use python3 -m venv .venv.
    py -3 -m venv .venv
    

    This command runs the Python venv module and creates a virtual environment in a folder named ".venv". Typically, .gitignore files have a ".venv" entry so that the virtual environment doesn't get checked in with your code checkins.

  4. Activate the virtual environment:

    source .venv/Scripts/activate
    

Once you activate that environment (which Visual Studio Code does automatically), running pip install installs a library into that environment only. Python code running in a virtual environment uses the specific package versions installed into that virtual environment. Using different virtual environments allows different applications to use different versions of a package, which is sometimes required. To learn more about virtual environments, see Virtual Environments and Packages in the Python docs.

For example, if your requirements are in a requirements.txt file, then inside the activated virtual environment, you can install them with:

pip install -r requirements.txt

Next step