Azure 功能:在消费计划上安装 Python 模块和扩展

问题描述

我正在尝试使用 Azure 函数运行 python 脚本.我在 App Services 计划下成功更新了 python 版本并在 Azure 函数上安装了模块,但我需要在 Consumption 计划下使用它,因为我的脚本每天只执行一次,而且只执行几分钟,所以我只想支付执行的时间.请参阅:

你现在应该可以开始你想要的了.

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

阅读之前的评论,看来你需要 numpy.我刚刚测试,我能够安装 1.12.1 没有任何问题.

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/

解决方案

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')))

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.

相关文章