Tutorial: Create and deploy serverless Azure Functions in Python with Visual Studio Code
In this article, you use Visual Studio Code and the Azure Functions extension to create a serverless HTTP endpoint with Python and to also add a connection (or "binding") to storage.
Azure Functions runs your code in a serverless environment without needing to provision a virtual machine or publish a web app. The Azure Functions extension for Visual Studio Code greatly simplifies the process of using Functions by automatically handling many configuration concerns.
If you encounter issues with any of the steps in this tutorial, we'd love to hear the details. Use the This page feedback button at the end of each article.
For a demonstration video, see Build Azure Functions with VS Code (youtube.com) from virtual PyCon 2020. You might also be interested in the longer session, Easy data processing with Azure Functions (youtube.com).
Configure your environment
- An Azure subscription.
- The Azure Functions Core Tools.
- Visual Studio Code with the Azure Functions extension.
If you don't have an Azure subscription, sign up now for a free 30-day account with $200 in Azure credits to try out any combination of services.
Azure Functions Core Tools
Install the Azure Functions Core Tools by following the instructions for your operating system on Work with Azure Functions Core Tools. Ignore the comments in the article about the Chocolatey package manager, which are not necessary to complete this tutorial.
When installing Node.js, use the default options and do not select the option to automatically install necessary tools. Also be sure to use the
-g option with the
npm install commands so that the Core Tools are available to subsequent commands.
The Core Tools are written in .NET Core, and the Core Tools package is best installed using the Node.js package manager, npm, which is why you need to install .NET Core and Node.js at present, even for working with Azure Functions in Python. You can, however bypass the .NET Core requirement using "extension bundles" as described in the aforementioned documentation. Whatever the case, you need install these components only once, after which Visual Studio Code automatically prompts you to install any updates.
Visual Studio Code, Python, and the Azure Functions extension
Install the following software:
A 64-bit version of Python 3.6, 3.7, or 3.8 as required by Azure Functions. Install Python from python.org. When installing, select Add Python 3.x to PATH use the default options by selecting the Install Now option. On Windows, also select Disable Path length limit at the end of the process.
The Python extension as described on Visual Studio Code Python Tutorial - Prerequisites.
The Azure Functions extension is included with the the Azure Tools extension pack.
Sign in to Azure
Once you've installed the Azure extension, sign into your Azure account by navigating to the Azure explorer, select Sign in to Azure under Functions, and follow the prompts in the browser.
After signing in, verify that the status bar says Azure: Signed In and your subscription(s) appears in the Azure explorer:
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
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
Verify your environment
To verify that all the Azure Functions tools are installed, open the Visual Studio Code Command Palette (F1), select the Terminal: Create New Integrated Terminal command, and once the terminal opens, run the command
The output that starts with the Azure Functions logo (you need to scroll the output upwards) indicates that the Azure Functions Core Tools are present.
func command isn't recognized, then run
npm install -g azure-functions-core-tools again and verify that the install succeeds. Make sure also that you use the
-g switch with the install command; otherwise npm installs the package in the current folder only.
func command works through the func.cmd file that's installed in the Node.js global folder. To see the location of this folder, run
npm -l and examine the location at the end of the output.