英文:
How can I use the ZBar libraries in a python Azure function?
问题
I am attempting to write a simple Azure Function app in Python.
My function app work fine in the local simulator but when I attempt to publish it to Azure, it fails with this error:
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.
My understanding is that the ZBar libraries cannot be found by the runtime environment (Lunix x64 for Azure Functions).
My "requirements.txt" file does not contain zbar
but it does contain pyzbar
which is the wrapper I am using.
My question is: how can I deploy the necessary libraries in the host in Azure functions?
英文:
I am attempting to write a simple Azure Function app in Python.
My function app work fine in the local simulator but when I attempt to publish it to Azure, it fails with this error:
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.
My understanding is that the ZBar libraries cannot be found by the runtime environment (Lunix x64 for Azure Functions).
My "requirements.txt" file does not contain zbar
but it does contain pyzbar
which is the wrapper I am using.
My question is: how can I deploy the necessary libraries in the host in Azure functions?
答案1
得分: 1
I tried adding pyzbar[zbar] to my Azure Function's requirements.txt and successfully deployed the function app in Azure Portal.
My requirements.txt:
I used the following commands to deploy my function app to Azure from my local environment:
func azure functionapp publish <function-app-name>
func azure functionapp publish <app_name> --build remote
The remote build includes the pyzbar package in the deployment.
Output:
pyzbar got installed successfully as shown below:
The function was deployed in Azure Portal, as shown below:
Portal:
Function:
Since Zbar library cannot be installed with pip due to its dependency on the Zbar package, you can create a custom function docker image and install the Zbar library as follows:
Custom docker image:
FROM mcr.microsoft.com/azure-functions/python:3.0
# Install zbar libraries
RUN apt-get update && apt-get install -y libzbar0
Build your docker image:
docker build -t <your_image_name> .
Update your function app to use the docker image by referencing it in Host.json like this:
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
},
"functions": [
{
"name": "MyFunction",
"docker": {
"image": "<your_image_name>"
}
}
]
}
References:
Refer to the following posts 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
英文:
I tried adding pyzbar[zbar] in the requirements.txt of my Azure Function and deployed the function app in Azure Portal successfully like below:-
My requirements.txt
I used the below commands to deploy my function app to azure from local env:-
func azure functionapp publish <function-app-name>
func azure functionapp publish <app_name> --build remote
The remote build will include pyzbar package in the deployment
Output:-
pyzbar got installed successfully like below:-
Function got deployed in Azure Portal, Refer below:-
Portal:-
Function:-
As Zbar library cannot be installed with pip cause it has a dependency on the Zbar package that needs to be installed from here- https://zbar.sourceforge.net/download.html, You can create a custom function docker image and install your Zbar library like below :-
Custom docker image :-
FROM mcr.microsoft.com/azure-functions/python:3.0
# Install zbar libraries
RUN apt-get update \
&& apt-get install -y libzbar0
Build your docker image:-
docker build -t <your_image_name> .
Update your function app to use the docker image by referencing it in Host.json like below:-
{
"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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论