Get started with Python on Azure

Use this document as a checklist and a guide as you begin developing Python applications that will be hosted in the cloud or utilize cloud services. If you follow the links and instructions in this document, you'll:

  • have a fundamental understanding of what the cloud is and how you design your application with the cloud in mind.
  • setup your local development environment including the tools and libraries you'll need to build cloud-based applications.
  • understand the workflow when developing cloud-based applications.

Phase 1: Learn concepts

If you are new to developing applications for the cloud, this short series of articles with videos will help you get up to speed quickly.

Once you understand the basics of developing applications for the cloud, you will want to set up your development environment and follow a Quickstart or Tutorial to build your first app.

Phase 2: Configure your local Python environment for Azure development

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.8 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.

After installing the Azure CLI, 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 -m venv .venv
    

    This command runs the Python venv module and creates a virtual environment in a folder ".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:

    .venv\Scripts\activate
    

    Note

    If you're using Windows Command shell, activate the virtual environment with .venv\Scripts\activate. If you're using Git Bash in Visual Studio Code on Windows, use the command source .venv/Scripts/activate instead.

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

Phase 3: Understand the Azure development workflow

Previous article: provisioning, accessing, and managing resources

Now that you understand Azure's model of services and resources, you can understand the overall flow of developing cloud applications with Azure: provision, code, test, deploy, and manage.

Step Primary tools Activities
Provision Azure CLI, Azure portal, VS Code Azure Tools extensions, Cloud Shell, Python scripts using Azure SDK management libraries Create resource groups and create resources in those groups; configure resources to be ready for use from app code and/or ready to receive Python code in deployments.
Code Code editor (such as Visual Studio Code and PyCharm), Azure SDK client libraries, reference documentation Write Python code using the Azure SDK client libraries to interact with provisioned resources.
Test Python runtime, debugger Run Python code locally against active cloud resources (typically dev or test resources rather than production resources). The code itself isn't yet hosted on Azure, which helps you debug and iterate quickly.
Deploy VS Code, Azure CLI, GitHub Actions, Azure Pipelines Once code has been tested locally, deploy it to an appropriate Azure hosting service where the code itself can run in the cloud. Deployed code typically runs against staging or production resources.
Manage Azure CLI, Azure portal, VS Code, Python scripts, Azure Monitor Monitor app performance and responsiveness, make adjustments in production environment, migrate improvements back to dev environment for the next round of provisioning and development.

Step 1: Provision and configure resources

As described in the previous article of this series, the first step in developing any application is to provision and configure the resources that make up the target environment for your application.

Provisioning begins by creating a resource group in a suitable Azure region. You can create a resource group through the Azure portal, VS Code with Azure Tools extensions, the Azure CLI, or with a custom script that uses the Azure SDK management libraries (or REST API).

Within that resource group, you then provision and configure the individual resources you need, again using the portal, VS Code, the CLI, or the Azure SDK. (Again, review the Azure developer's guide for an overview of available resource types.)

Configuration includes setting access policies that control what identities (service principals and/or application IDs) are able to access those resources. Access policies are managed through Azure Role-Based Access Control (RBAC); some services have more specific access controls as well. As a cloud developer working with Azure, make sure to familiarize yourself with Azure RBAC because you use it with just about any resource that has security concerns.

For most application scenarios, you typically create provisioning scripts with the Azure CLI and/or Python code using the Azure SDK management libraries. Such scripts describe the totality of your application's resource needs (essentially defining the custom cloud computer to which you're deploying the application). A script enables you to easily recreate the same set of resources within different environment like development, test, staging, and production. When you automate, you can avoid manually performing many repeated steps in Azure portal or VS Code. Such scripts also make it easy to provision an environment in a different region, or to use different resource groups. If you also maintain these scripts in source control repositories, you also have full auditing and change history.

Step 2: Write your app code to use resources

Once you've provisioned the resources you need for your application, you write the application code to work with the run time aspects of those resources.

For example, in the provisioning step you might have created an Azure storage account, created a blob container within that account, and set access policies for the application on that container. This provisioning process is demonstrated in Example - Provision Azure Storage. From your code, you can then authenticate with that storage account and then create, update, or delete blobs within that container. This run time process is demonstrated in Example - Use Azure Storage. Similarly, you might have provisioned a database with a schema and appropriate permissions (as demonstrated in Example - Provision a database), so that your application code can connect to the database and perform the usual create-read-update-delete queries.

App code typically uses environment variables to identify the names and URLs of the resources to use. Environment variables allow you to easily switch between cloud environments (dev, test, staging, and production) without any changes to the code. The various Azure services that host application code provide a means to define the necessary variables. For example, in Azure App Service (to host web apps) and Azure Functions (serverless compute for Azure), you define application settings through the Azure portal, VS Code, or Azure CLI, which then appear to your code as environment variables.

As a Python developer, you'll likely write your application code in Python using the Azure SDK client libraries for Python. That said, any independent part of a cloud application can be written in any supported language. If you're working on a team using multiple programming languages, it's possible that some parts of the application use Python, some JavaScript, some Java, and others C#.

Application code can use the Azure SDK management libraries to perform provisioning and management operations as needed. Provisioning scripts, similarly, can use the SDK client libraries to initialize resources with specific data, or perform housekeeping tasks on cloud resources even when those scripts are run locally.

Step 3: Test and debug your app code locally

Developers typically like to test app code on their local workstations before deploying that code to the cloud. Testing app code locally means that you're typically accessing other resources that you've already provisioned in the cloud, such as storage, databases, and so forth. The difference is that you're not yet running the app code itself within a cloud service.

By running the code locally, you can also take full advantage of debugging features offered by tools such as Visual Studio Code and manage your code in a source control repository.

You don't need to modify your code at all for local testing: Azure fully supports local development and debugging using the same code you deploy to the cloud. Environment variables are again the key: in the cloud, your code can access the hosting resource's settings as environment variables. When you create those same environment variables locally, the same code runs without modification. This pattern works for authentication credentials, resource URLs, connection strings, and any number of other settings, making it easy to use resources in a development environment when running code locally and production resources once the code is deployed to the cloud.

Step 4: Deploy your app code to Azure

Once you've tested your code locally, you're ready to deploy the code to the Azure resource that you've provisioned to host it. For example, if you're writing a Django web app, you either deploy that code to a virtual machine (where you provide your own web server) or to Azure App Service (which provides the web server for you). Once deployed, that code is running on the server rather than on your local machine, and can access all the Azure resources for which it's authorized.

As noted in the previous section, in typical development processes you first deploy your code to the resources you've provisioned in a development environment. After a round of testing, you deploy your code to resources in a staging environment, making the application available to your test team and perhaps preview customers. Once you're satisfied with the application's performance, you can deploy the code to your production environment. All of these deployments can also be automated through continuous integration and continuous deployment using Azure Pipelines and GitHub Actions.

However you do it, once the code is deployed to the cloud, it truly becomes a cloud application, running entirely on the server computers in Azure data centers.

Step 5: Manage, monitor, and revise

After deployment, you want to make sure the application is performing as it should, responding to customer requests and using resources efficiently (and at the lowest cost). You can manage how Azure automatically scales your deployment as needed, and you can collect and monitor performance data with Azure portal, VS Code, the Azure CLI, or custom scripts written with the Azure SDK libraries. You can then make real-time adjustments to your provisioned resources to optimize performance, again using any of the same tools.

Monitoring gives you insight about how you might restructure your cloud application. For example, you may find that certain portions of a web app (such as a group of API endpoints) are used only occasionally in comparison to the primary parts. You could then choose to deploy those APIs separately as serverless Azure Functions. As functions, they have their own backing compute resources that don't compete with the main application but cost only pennies per month. Your main application then becomes more responsive to more customers without having to scale up to a higher-cost tier.

Next steps

You're now familiar with the basic structure of Azure and the overall development flow: provision resources, write and test code, deploy the code to Azure, and then monitor and manage those resources.