在python中部署azure函数不会安装requirements.txt中的依赖项

polhcujo  于 2023-03-31  发布在  Python
关注(0)|答案(2)|浏览(155)

我有一个简单的python脚本,可以按照这里的说明部署为azure函数。
当脚本被触发时(为blob上传的事件网格事件设置),它会遇到一个运行时错误:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.imageeventgridtrigger --->
  Microsoft.Azure.WebJobs.Script.Rpc.RpcException: Result: Failure
  Exception: ModuleNotFoundError: No module named 'numpy' Stack:   File
  "/azure-functions-host/workers/python/3.6/LINUX/X64/azure_functions_worker/dispatcher.py",
  line 242, in _handle__function_load_request
      func_request.metadata.entry_point)   File "/azure-functions-host/workers/python/3.6/LINUX/X64/azure_functions_worker/loader.py",
  line 66, in load_function
      mod = importlib.import_module(fullmodname)   File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in
  import_module
      return _bootstrap._gcd_import(name[level:], package, level)   File "/home/site/wwwroot/imageeventgridtrigger/__init__.py", line 3, in
  <module>
      import numpy as np    at Microsoft.Azure.WebJobs.Script.Description.OutOfProcInvoker.InvokeCore(Object[]
  parameters, FunctionInvocationContext context) in
  /src/azure-functions-host/src/WebJobs.Script/Description/OutOfProc/OutOfProcInvoker.cs:line
  82*

[截取其余跟踪]部署是通过vscode集成进行的。根据日志输出,requirements文件似乎部分执行:

10:09:44 AM manuscriptfunctionapp: Python Version: /opt/python/3.6.9/bin/python3 10:09:44 AM manuscriptfunctionapp:
  Running pip install... 10:09:47 AM manuscriptfunctionapp:
  [04:39:47+0000] Collecting azure-functions (from -r requirements.txt
  (line 1)) 10:09:48 AM manuscriptfunctionapp: [04:39:48+0000]  
  Downloading
  https://files.pythonhosted.org/packages/68/b3/535af50791b2fcda75d3e18820588885686a33d8a13f83af6dcf74094cf4/azure_functions-1.0.4-py3-none-any.whl
  (108kB) 10:09:48 AM manuscriptfunctionapp: [04:39:48+0000] Collecting
  numpy==1.16.4 (from -r requirements.txt (line 2)) 10:09:50 AM
  manuscriptfunctionapp: [04:39:50+0000]   Downloading
  https://files.pythonhosted.org/packages/87/2d/e4656149cbadd3a8a0369fcd1a9c7d61cc7b87b3903b85389c70c989a696/numpy-1.16.4-cp36-cp36m-manylinux1_x86_64.whl
  (17.3MB) 10:09:52 AM manuscriptfunctionapp: [04:39:52+0000] Collecting
  matplotlib==3.1.0 (from -r requirements.txt (line 3)) 10:09:53 AM
  manuscriptfunctionapp: [04:39:53+0000]   Downloading
  https://files.pythonhosted.org/packages/da/83/d989ee20c78117c737ab40e0318ea221f1aed4e3f5a40b4f93541b369b93/matplotlib-3.1.0-cp36-cp36m-manylinux1_x86_64.whl
  (13.1MB) 10:10:07 AM manuscriptfunctionapp: Waiting for long running
  command to finish... 10:10:25 AM manuscriptfunctionapp: App container
  will begin restart within 10 seconds. 10:10:44 AM
  manuscriptfunctionapp: Syncing triggers... 10:10:48 AM: Deployment to
  "manuscriptfunctionapp" completed. 10:10:48 AM manuscriptfunctionapp:
  Querying triggers...

requirements文件有其他依赖项,但它们不会出现在日志输出中(直到第3行!)。尝试多次部署,但结果相同。Requirements.txt:

azure-functions
numpy==1.16.4
matplotlib==3.1.0
opencv_python==4.1.1.26
scikit_image==0.15.0
scipy==1.2.1
skimage==0.0

Python脚本:

mport json
import logging
import numpy as np
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import statistics
import cv2
import os
import scipy
import scipy.signal as ss
import skimage
from skimage.filters import (threshold_otsu, threshold_niblack, threshold_sauvola)
from skimage.restoration import denoise_nl_means, estimate_sigma

import azure.functions as func

def main(event: func.EventGridEvent):
    result = json.dumps({
        'id': event.id,
        'data': event.get_json(),
        'topic': event.topic,
        'subject': event.subject,
        'event_type': event.event_type,
    })

    logging.info('Python EventGrid trigger processed an event: %s', result)
    logging.info('cv2 %s ', cv2.__version__)
    logging.info('matplotlib %s ', matplotlib.__version__)
    logging.info('scipy %s ', scipy.__version__)
    logging.info('skimage %s', skimage.__version__)

如果要求中只提到azure-functions,则部署和触发器工作正常。这是针对Always On应用程序服务计划,并且正在Linux上进行部署。部署出现了什么问题?

oogrdqng

oogrdqng1#

解决了这个问题。requirements.txt文件中有一个虚假的skimage条目。python中的导入引用skimage,但requirements必须是scikit-image。因为我通过pipereqs生成了文件,所以我错过了这一点。
奇怪的是,发现部署日志并不容易。Azure func在vscode终端中的默认输出没有完整的堆栈。但在Azure扩展选项卡下,有详细的部署日志可用于功能应用程序,其中包含调试所需的信息。

9w11ddsr

9w11ddsr2#

在我的例子中,我只是将文件从Azure Dev Ops(ADO)存储库转储到Azure Function。
正确的步骤是首先构建代码,然后才部署它。This是一种制作ADO管道的方法,该管道构建Python代码,然后将 drop 发布到Azure Functions。

相关问题