This is a quickstart on how you create and deploy a Python function on Azure Functions 2.X using the Azure Functions Core Tools.
- Install
Python 3.6
- Install Azure Core Tools version 2.x (the latest one)
# Install the latest Azure Core Tools version 2.x
npm install -g azure-functions-core-tools
# If it's on macOS, you can install with homebrew
brew tap azure/functions
brew install azure-functions-core-tools
$ func init v2functions --worker-runtime python
List all python functions
$ func templates list
Python Templates:
Azure Blob Storage trigger
Azure Cosmos DB trigger
Azure Event Grid trigger
Azure Event Hub trigger
HTTP trigger
Azure Queue Storage trigger
Azure Service Bus Queue trigger
Azure Service Bus Topic trigger
Timer trigger
...
Then, create a python function from templates
# First of all, move to pythoh functions project top
$ cd v2functions
# Http Trigger functions
$ func new --language python --template "HttpTrigger" --name HttpTriggerPY
# Blob Trigger functions
$ func new --language python --template "Azure Blob Storage trigger" --name BlobTriggerPY
# Cosmos DB Trigger functions
$ func new --language python --template "Azure Cosmos DB trigger" --name CosmosdbTriggerPY
Create a virtual environment directory at the top of function directory
cd functions
python3.6 -m venv .env
source .env/bin/activate
After activate your virtual enviroment for the function development, install packages you use in your functions (For example, numpy
)
pip install --upgrade pip
pip install numpy
...
When you develop locally using the Azure Functions Core Tools or VS Code, I guess you simply install your required python packages uinsg pip
. It's OK in developing locally but when it comes to the production deployment, Please make sure that all your dependencies are listed in the requirements.txt
, located at the root of your project directory. For example, here is a requirements.txt where I added my required packages and its version for my sample function (For minimum packages, please refer to this)
# Minimum packages for azure functions
azure-functions
azure-functions-worker
grpcio==1.14.1
grpcio-tools==1.14.1
protobuf==3.6.1
six==1.11.0
# Additional packages
numpy==1.15.4
Here is how you install packages listed in requirements.txt
pip install -r requirements.txt
Configure trigger, input and output binding with function.json
. Please see Azure Functions Python developer guide for the detail
In version 2.x of the Azure Functions runtime, you have to explicitly register the binding extensions that you use in your function app. To use extension bundles, update the host.json
file to include the following entry for extensionBundle:
host.json
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
[NOTE] As an alternative way, you can manually install extension bundles by running a command -
func extensions install
so appropritate binding extensions are installed inbin
directory. But if you already added the entry for extensionBundle inhost.json
like above, you don't need this.# change directory to a project directory cd functions # Manually install extension bundles using func command (Azure Core Tools) func extensions install
From inside the project directory (e.g. v2functions
), run:
func host start
For more detail, please refer to Local development Azure Functions Core Tools
APP_NAME="your function name"
func azure functionapp publish $APP_NAME
If you got ERROR like this, do publish with --build-native-deps
option
There was an error restoring dependencies.ERROR: cannot install vsts-cd-manager-1.0.2 dependency: binary dependencies without wheels are not supported. Use the --build-native-deps option to try building the binary dependenciesusing a Docker container.
Publishing with --build-native-deps
option:
func azure functionapp publish $APP_NAME --build-native-deps
If you're unable to import your modules, try publishing again using the --no-bundler
option ( See also this doc for more detail):
func azure functionapp publish $APP_NAME --build-native-deps --no-bundler