Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python 3 in Azure Functions

I'm aiming to get some Python 3 code running as an Azure Function, but I can't get Python 3 to work (I realize python support in Azure Functions is still experimental).

Here's what I've tried.

  1. Create a new Function App - I've given it a name, and left everything else as default.

  2. Created a Python HttpTrigger function, with the following code:

    import sys
    import os
    
    response = open(os.environ['res'], 'w')
    response.write(sys.version)
    response.close()    
    

Running this function gives an output of "2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]" as expected, because 2.7.8 is the default version of python that is installed in the Azure Functions environment.

  1. I then uploaded the embeddable version of python 3.6.1 to d:\site\tools using Kudu, as described in this Azure wiki page

When I run the function again, I get the output "3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]" - so all is good there.

However, if I restart the function app (or just leave it alone for a while so it gets shut down) then everything breaks. Running the function gives:

{
  "id": "dd7c4462-0d73-49e0-8779-67b15a9bba82",
  "requestId": "ff553805-501d-4ea6-93f6-7bd6fa445a37",
  "statusCode": 500,
  "errorCode": 0,
  "message": "Exception while executing function: Functions.HttpTriggerPython31 -> "
}

The logs show:

2017-11-09T17:37:04.988 Function started (Id=941e5bef-e5e0-4604-8533-dd2a5fcaddf0)
2017-11-09T17:37:05.348 Exception while executing function: Functions.HttpTriggerPython31. Microsoft.Azure.WebJobs.Script: .
2017-11-09T17:37:05.364 Function completed (Failure, Id=941e5bef-e5e0-4604-8533-dd2a5fcaddf0, Duration=363ms)

If I delete the python files from d:\site\tools, then then function starts working again, but runs with v2.7.8

However, I can get python 3.x to run from the Kudu console:

D:\home\site\tools>python -c "import sys;print(sys.version)"
3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)]

Does anyone else have Python 3 successfully running in Azure Functions? What do I need to do to get it working?

like image 508
roomaroo Avatar asked Nov 09 '17 17:11

roomaroo


People also ask

Does Azure support Python 3?

The support is available in all the public cloud regions. You can import, author, and run Python 3 runbooks in Azure or in a Hybrid Runbook Worker.

Can we use Python in Azure function?

While you can develop your Python based Azure Functions locally on Windows, Python is only supported on a Linux based hosting plan when running in Azure. See the list of supported operating system/runtime combinations.


1 Answers

For the moment there is an open issue :

  • Add ability to change Python version

But as a workaround, everything is explain here (All credits to the author: Murat Eken)

  1. Create your function.
  2. Installing a site extension with an alternative Python version and configuring the handler mapping to use that installation by default..

    2.1. Go to "Platform features > All Settings > Extensions > + Add

    2.2. Install the "Python 3.6.2 x86" extension.

    2.3. Go to "Platform features > Applications Settings

    2.4. Add an handler mapping:
    extension : fastCgi
    processor: D:\home\python362x86\python.exe
    arguments: D:\home\python362x86\wfastcgi.py

Azure Function - Handler mapping to use a specific python version

2.5 Add an application setting called WEBSITE_USE_PLACEHOLDER and set its value to 0. This is necessary to work around an Azure Functions issue that causes the Python extension to stop working after the function app is unloaded.

2.6. Save your app settings.

Here is the output of my function "3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)]"

you can automate these step using an ARM Template, here are the interesting part of the template:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
  "contentVersion": "1.0.0.0",
  "parameters": {
    //...
  },
  "variables": {
    // ...
  },
  "resources": [
    // ...
    {
      "type": "Microsoft.Web/sites",
      // ...
      "kind": "functionapp",
      "properties": {
        // ...
        "siteConfig": {
          "handlerMappings": [{
            "arguments": "D:\\home\\python362x86\\wfastcgi.py",
            "extension": "fastCgi",
            "scriptProcessor": "D:\\home\\python362x86\\python.exe"
          }]
          // ...
        },
        "resources": [{
          "name": "python362x86",
          "type": "siteextensions",
          "apiVersion": "2016-08-01",
          "properties": {}
          // ...
        }]
        // ...
      }
    }
  ]
}
like image 194
Thomas Avatar answered Sep 28 '22 01:09

Thomas