17

I am trying to run a python script with Azure functions. I had success updating the python version and installing modules on Azure functions under the App Services plan but I need to use it under the Consumption plan as my script will only execute once everyday and for only a few minutes, so I want to pay only for the time of execution. See: https://azure.microsoft.com/en-au/services/functions/

Now I'm still new to this but from my understanding the consumption plan spins up the vm and terminates it after your script has been executed unlike the App Service plan which is always on. I am not sure why this would mean that I can't have install anything on it. I thought that would just mean I have to install it every time I spin it up.

I have tried installing modules through the python script itself and the kudu command line with no success.

While under the app service plan it was simple, following this tutorial: https://prmadi.com/running-python-code-on-azure-functions-app/

mike
  • 881
  • 2
  • 10
  • 23

3 Answers3

37

On Functions Comsumption plan, Kudu extensions are not available. However, you can update pip to be able to install all your dependencies correctly:

  • Create your Python script on Functions (let's say NameOfMyFunction/run.py)
  • Open a Kudu console
  • Go to the folder of your script (should be d:/home/site/wwwroot/NameOfMyFunction)
  • Create a virtualenv in this folder (python -m virtualenv myvenv)
  • Load this venv (cd myenv/Scripts and call activate.bat)

Your shell should be now prefixed by (myvenv)

  • Update pip (python -m pip install -U pip)
  • Install what you need (python -m pip install flask)

Now in the Azure Portal, in your script, update the sys.path to add this venv:

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

enter image description here

You should be able to start what you want now.

(Reference: https://github.com/Azure/azure-sdk-for-python/issues/1044)

Edit: reading previous comment, it seems you need numpy. I just tested right now and I was able to install 1.12.1 with no issues.

Laurent Mazuel
  • 3,422
  • 13
  • 27
  • 1
    Thanks @Laurent Mazuel ! This worked perfectly and not only for numpy and pandas but for every module I tried. – mike May 16 '17 at 01:16
  • @Laurent Manuel, thanks for great instructions and your help in unblocking @mike! – Ling Toh May 16 '17 at 05:05
  • This approach works for me, but importing pandas slows my process way down. The import alone takes more than 20 seconds. Did I do something wrong or is this expected behaviour? – JrtPec Jun 14 '17 at 16:09
  • Please help, I run activate.bat but there is no prefix (myvenv) appear – Quang Hoàng Sep 21 '17 at 10:06
  • After installing external python module through the kudu console, Can I assume that it is gonna sclae properly ? If a new VM is spin up, the additional modules will also be installed ? – Thomas Oct 02 '17 at 03:50
  • Is it possible to leave the `requirements.txt` empty only with a dot and it will run a custom setup script before deploying to the cloud? – Matthias Herrmann Apr 13 '19 at 09:29
2

You may upload the modules for the Python version of your choice in Consumption Plan. Kindly refer to the instructions at this link: https://github.com/Azure/azure-webjobs-sdk-script/wiki/Using-a-custom-version-of-Python

Ling Toh
  • 2,404
  • 1
  • 16
  • 24
  • Thanks Ling. For Python 2.7.13 I can't find an embeddable zip file. Am I right in thinking that updating the version and installing the modules that my scripts need will need to be done every time I start up a script (and consequently a vm) under the consumption plan. – mike May 15 '17 at 01:49
  • 1
    Sorry I am not familiar with Python and will defer the question about locating an embeddable zip file to others. As for your second question, the modules will be loaded when your Function App is first instantiated. A Function App is a process that hosts all the Functions. Under the Consumption Plan, each Function App has 5 mins to complete all its workloads before it is forcibly terminated. Your Function may be triggered and executed many times during this 5 min timeframe but the Python modules would have been loaded only once on instantiation of the Function App process. – Ling Toh May 15 '17 at 02:55
  • When trying to install numpy from inside my script I am met with: 2017-05-15T06:33:53.919 WindowsError: [Error 5] Access is denied: 'D:\\Python27\\Lib\\site-packages\\numpy' – mike May 15 '17 at 06:34
  • 1
    I have seen this error before. Unfortunately, you will not be able to install numpy on that path. Kindly see the following related SO thread: http://stackoverflow.com/a/40474485/6465830 – Ling Toh May 15 '17 at 07:03
0

This is what worked for me:

Dislaimer: I use C# Function that includes Python script execution, using command line with System.Diagnostics.Process class.

  • Add relevant Python extension for the Azure Function from Azure Portal: Platform Features -> Development Tools -> Extensions
    It installed python to D:\home\python364x86 (as seen from Kudu console)

  • 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.
    See: Using Python 3 in Azure Functions question.

  • Install the packages from Kudu CMD line console using pip install ...
    (in my case it was pip install pandas)

Dima G
  • 1,945
  • 18
  • 22