Using Python in Azure Functions

Changing versions

  • Check the current version that is installed. Inside the Kudu command prompt: python -c "import sys; print(sys.version)"
  • If you need to upgrade go to ‘Site Extensions’ in Kudu > Gallery > Search for Python > Install it and copy the path it installs to (see below):
Install the Python version you need in Kudu Site Extensions. Remember to copy the path!
  • Go back to the function app in the Azure portal and go to Application Settings > Scroll down to the ‘Handler mappings’ section > Click ‘Add new handler mapping’ > Enter the following:
Handler mapping options. Replace the path shown with the path you copied from the previous step.
  • Rerun the version check to make sure it has worked.

Installing packages

  • In Kudu change the current directory to your function app directory. This is usually d:/home/site/wwwroot/<function app name>
  • Install the virtualenv package python -m pip install virtualenv
  • Create a new virtual environment: python -m virtualenv <env name>
  • Change into the <env name>/Scripts directory and run activate.bat to activate the virtual environment (the command line will be prefixed with the environment name so you know this has worked).
  • Update pip if required: python -m pip install -U pip
  • Install your packages!

Update your code to use the virtual environment

import sys
import os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'myenv/Lib/site-packages')))

--

--

--

Me: https://poc275.me

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Peter O'Connor

Peter O'Connor

Me: https://poc275.me

More from Medium

SQL Azure Pricing Overview

Creating an Azure Function with a Blob Trigger

Query SQL Server using Powershell to output a CSV

Filter/Map/Reduce JSON data in PowerShell

Code Completion in Vs Code for JSON Objects in PowerShell