Visual Studio Code Python Pip



  1. Visual Studio Code Python Pip
  2. Visual Studio For Python

Close all instances of VS Code; Next, launch VS Code from that same terminal (command window) session (venv) ter @minal: $ code. Python Version used for debugging. Details on configuration settings for debugging can be found here Debugging. Configuring the version of the python executable is no longer necessary. Visual Studio provides a UI to manage packages in your Python environments. View environments. Select the View Other Windows Python Environments menu command. The Python Environments window opens as a peer to Solution Explorer and shows the different environments available to you. The list shows both environments that you installed using the Visual Studio installer and those you. The April 2021 release of the Python Extension for Visual Studio Code includes a preview of support for Poetry environments, improved completions for PyTorch 1.8.1 when using Pylance, as well enhancements to the Data Viewer. Visual Studio IDE analyzes your code to make suggestions. Manage 3rd party libraries. Interativey debug on Windows and Linux. Use our pip, PyPI and virtual environment support to manage your projects and dependencies. Python Tools for Visual Studio.

-->

Durable Functions is an extension of Azure Functions that lets you write stateful functions in a serverless environment. The extension manages state, checkpoints, and restarts for you.

In this article, you learn how to use the Visual Studio Code Azure Functions extension to locally create and test a 'hello world' durable function. This function will orchestrate and chain together calls to other functions. You then publish the function code to Azure.

Prerequisites

To complete this tutorial:

  • Install Visual Studio Code.

  • Install the Azure Functions VS Code extension.

  • Make sure you have the latest version of the Azure Functions Core Tools.

  • Durable Functions require an Azure storage account. You need an Azure subscription.

  • Make sure that you have version 3.6, 3.7, or 3.8 of Python installed.

If you don't have an Azure subscription, create a free account before you begin.

Pipe

Create your local project

In this section, you use Visual Studio Code to create a local Azure Functions project.

  1. In Visual Studio Code, press F1 (or Ctrl/Cmd+Shift+P) to open the command palette. In the command palette, search for and select Azure Functions: Create New Project....

  2. Choose an empty folder location for your project and choose Select.

  3. Following the prompts, provide the following information:

    PromptValueDescription
    Select a language for your function app projectPythonCreate a local Python Functions project.
    Select a versionAzure Functions v3You only see this option when the Core Tools aren't already installed. In this case, Core Tools are installed the first time you run the app.
    Python versionPython 3.6, 3.7, or 3.8VS Code will create a virtual environment with the version you select.
    Select a template for your project's first functionSkip for now
    Select how you would like to open your projectOpen in current windowReopens VS Code in the folder you selected.

Visual Studio Code installs the Azure Functions Core Tools, if needed. It also creates a function app project in a folder. This project contains the host.json and local.settings.json configuration files.

A requirements.txt file is also created in the root folder. It specifies the Python packages needed to run your function app.

Install azure-functions-durable from PyPI

When you created the project, the Azure Functions VS Code extension automatically created a virtual environment with your selected Python version. You will activate the virtual environment in a terminal and install some dependencies required by Azure Functions and Durable Functions.

  1. Open requirements.txt in the editor and change its content to the following:

  2. Open the editor's integrated terminal in the current folder (Ctrl+Shift+`).

  3. In the integrated terminal, activate the virtual environment in the current folder:

    Linux or macOS

    Windows

  4. In the integrated terminal where the virtual environment is activated, use pip to install the packages you just defined:

Visual Studio Code Python Pip

Create your functions

A basic Durable Functions app contains three functions:

  • Orchestrator function - describes a workflow that orchestrates other functions.
  • Activity function - called by the orchestrator function, performs work, and optionally returns a value.
  • Client function - a regular Azure Function that starts an orchestrator function. This example uses an HTTP triggered function.

Orchestrator function

You use a template to create the durable function code in your project.

  1. In the command palette, search for and select Azure Functions: Create Function....

  2. Following the prompts, provide the following information:

    PromptValueDescription
    Select a template for your functionDurable Functions orchestratorCreate a Durable Functions orchestration
    Provide a function nameHelloOrchestratorName of your durable function

You've added an orchestrator to coordinate activity functions. Open HelloOrchestrator/__init__.py to see the orchestrator function. Each call to context.call_activity invokes an activity function named Hello.

Next, you'll add the referenced Hello activity function.

Visual Studio For Python

Activity function

  1. In the command palette, search for and select Azure Functions: Create Function....

  2. Following the prompts, provide the following information:

    PromptValueDescription
    Select a template for your functionDurable Functions activityCreate an activity function
    Provide a function nameHelloName of your activity function

You've added the Hello activity function that is invoked by the orchestrator. Open Hello/__init__.py to see that it takes a name as input and returns a greeting. An activity function is where you'll perform actions such as making a database call or performing a computation.

Finally, you'll add an HTTP triggered function that starts the orchestration.

Client function (HTTP starter)

Visual
  1. In the command palette, search for and select Azure Functions: Create Function....

  2. Following the prompts, provide the following information:

    PromptValueDescription
    Select a template for your functionDurable Functions HTTP starterCreate an HTTP starter function
    Provide a function nameDurableFunctionsHttpStartName of your activity function
    Authorization levelAnonymousFor demo purposes, allow the function to be called without authentication

You've added an HTTP triggered function that starts an orchestration. Open DurableFunctionsHttpStart/__init__.py to see that it uses client.start_new to start a new orchestration. Then it uses client.create_check_status_response to return an HTTP response containing URLs that can be used to monitor and manage the new orchestration.

You now have a Durable Functions app that can be run locally and deployed to Azure.

Test the function locally

Azure Functions Core Tools lets you run an Azure Functions project on your local development computer. If you don't have it installed, you're prompted to install these tools the first time you start a function from Visual Studio Code.

  1. To test your function, set a breakpoint in the Hello activity function code (Hello/__init__.py). Press F5 or select Debug: Start Debugging from the command palette to start the function app project. Output from Core Tools is displayed in the Terminal panel.

    Note

    Refer to the Durable Functions Diagnostics for more information on debugging.

  2. Durable Functions requires an Azure Storage account to run. When VS Code prompts you to select a storage account, choose Select storage account.

  3. Following the prompts, provide the following information to create a new storage account in Azure.

    PromptValueDescription
    Select subscriptionname of your subscriptionSelect your Azure subscription
    Select a storage accountCreate a new storage account
    Enter the name of the new storage accountunique nameName of the storage account to create
    Select a resource groupunique nameName of the resource group to create
    Select a locationregionSelect a region close to you
  4. In the Terminal panel, copy the URL endpoint of your HTTP-triggered function.

  5. Using your browser, or a tool like Postman or cURL, send an HTTP request to the URL endpoint. Replace the last segment with the name of the orchestrator function (HelloOrchestrator). The URL should be similar to http://localhost:7071/api/orchestrators/HelloOrchestrator.

    The response is the initial result from the HTTP function letting you know the durable orchestration has started successfully. It is not yet the end result of the orchestration. The response includes a few useful URLs. For now, let's query the status of the orchestration.

  6. Copy the URL value for statusQueryGetUri and paste it in the browser's address bar and execute the request. Alternatively you can also continue to use Postman to issue the GET request.

    The request will query the orchestration instance for the status. You should get an eventual response, which shows the instance has completed, and includes the outputs or results of the durable function. It looks like:

  7. To stop debugging, press Shift+F5 in VS Code.

After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.

Sign in to Azure

Before you can publish your app, you must sign in to Azure.

  1. If you aren't already signed in, choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose Sign in to Azure.... If you don't already have one, you can Create a free Azure account. Students can create a free Azure account for Students.

    If you're already signed in, go to the next section.

  2. When prompted in the browser, choose your Azure account and sign in using your Azure account credentials.

  3. After you've successfully signed in, you can close the new browser window. The subscriptions that belong to your Azure account are displayed in the Side bar.

Publish the project to Azure

In this section, you create a function app and related resources in your Azure subscription and then deploy your code.

Important

Publishing to an existing function app overwrites the content of that app in Azure.

  1. Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose the Deploy to function app... button.

  2. Provide the following information at the prompts:

    • Select folder: Choose a folder from your workspace or browse to one that contains your function app. You won't see this if you already have a valid function app opened.

    • Select subscription: Choose the subscription to use. You won't see this if you only have one subscription.

    • Select Function App in Azure: Choose - Create new Function App. (Don't choose the Advanced option, which isn't covered in this article.)

    • Enter a globally unique name for the function app: Type a name that is valid in a URL path. The name you type is validated to make sure that it's unique in Azure Functions.

    • Select a location for new resources: For better performance, choose a region near you.

    The extension shows the status of individual resources as they are being created in Azure in the notification area.

  3. When completed, the following Azure resources are created in your subscription, using names based on your function app name:

    • A resource group, which is a logical container for related resources.
    • A standard Azure Storage account, which maintains state and other information about your projects.
    • A consumption plan, which defines the underlying host for your serverless function app.
    • A function app, which provides the environment for executing your function code. A function app lets you group functions as a logical unit for easier management, deployment, and sharing of resources within the same hosting plan.
    • An Application Insights instance connected to the function app, which tracks usage of your serverless function.

    A notification is displayed after your function app is created and the deployment package is applied.

    Tip

    By default, the Azure resources required by your function app are created based on the function app name you provide. By default, they are also created in the same new resource group with the function app. If you want to either customize the names of these resources or reuse existing resources, you need to instead publish the project with advanced create options.

  4. Select View Output in this notification to view the creation and deployment results, including the Azure resources that you created. If you miss the notification, select the bell icon in the lower right corner to see it again.

Test your function in Azure

  1. Copy the URL of the HTTP trigger from the Output panel. The URL that calls your HTTP-triggered function should be in this format: http://<functionappname>.azurewebsites.net/api/orchestrators/HelloOrchestrator

  2. Paste this new URL for the HTTP request into your browser's address bar. You should get the same status response as before when using the published app.

Next steps

You have used Visual Studio Code to create and publish a Python durable function app.