我正在尝试用 Python 编写一个简单的 Azure Function 应用程序。
我的函数应用程序在本地模拟器中工作正常,但当我尝试将其发布到 Azure 时,它失败并出现以下错误:
Exception while executing function: Functions.ProcessPDF Result: Failure
Exception: ImportError: Unable to find zbar shared library. Please check the requirements.txt file for the missing module.
我的理解是运行时环境(Lunix x64 for Azure Functions)无法找到 ZBar 库。
我的“requirements.txt”文件不包含 zbar
,但它包含 pyzbar
,这是我正在使用的包装器。
我的问题是:如何在 Azure 函数的主机中部署必要的库?
最佳答案
我尝试在 Azure Function 的requirements.txt 中添加 pyzbar[zbar] 并在 Azure 门户中成功部署函数应用,如下所示:-
我的要求.txt
我使用以下命令将我的函数应用程序从本地环境部署到 azure:-
func azure functionapp publish <function-app-name>
func azure functionapp publish <app_name> --build remote
远程构建将在部署中包含 pyzbar 包
输出:-
pyzbar 已成功安装,如下所示:-
功能已部署在Azure Portal中,请参阅以下内容:-
门户:-
功能:-
由于 Zbar 库无法通过 pip 安装,因为它依赖于需要从此处安装的 Zbar 软件包 - https://zbar.sourceforge.net/download.html ,您可以创建自定义功能 docker 镜像并安装 Zbar 库,如下所示 :-
自定义 Docker 镜像:-
FROM mcr.microsoft.com/azure-functions/python:3.0
# Install zbar libraries
RUN apt-get update \
&& apt-get install -y libzbar0
构建你的 Docker 镜像:-
docker build -t <your_image_name> .
通过在 Host.json 中引用 Docker 镜像来更新您的函数应用,如下所示:-
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
},
"functions": [
{
"name": "MyFunction",
"docker": {
"image": "<your_image_name>"
}
}
]
}
References:-
Refer the post's below for more insights:-
python - ImportError: Unable to find zbar shared library on Flask - Stack Overflow By Mise Javier Buzzi stefan Kautuk Dwivedi
Add support to load library externally · Issue #40 · NaturalHistoryMuseum/pyzbar · GitHub By nickovs
关于python - 如何在 python Azure 函数中使用 ZBar 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75948875/