gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:07:55 26 4
gpt4 key购买 nike

我有一个简单的 python 脚本,可以按照说明部署为 azure 函数 here .

当脚本被触发时(为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 集成进行的。根据日志输出,需求文件似乎已部分执行:

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...

需求文件具有其他依赖项,但它们不会出现在日志输出中(仅到第 3 行!)。尝试部署多次但结果相同。要求.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 上进行。部署出了什么问题?

最佳答案

解决了这个问题。 requirements.txt 文件中有一个虚假的 skimage 条目。 python中的导入引用skimage,但要求必须是scikit-image。由于我通过 Pipereqs 生成了该文件,所以我错过了这一点。

奇怪的是,发现部署日志并不容易。 azure func 的 vscode 终端中的默认输出没有完整堆栈。但在 Azure 扩展选项卡下,有可用于具有调试所需信息的函数应用的详细部署日志。

关于python - 在Python中部署azure函数不会安装requirements.txt中的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58704759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com