你可以在 Python Azure 函数中如何使用 ZBar 库?

huangapple go评论78阅读模式
英文:

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:

你可以在 Python Azure 函数中如何使用 ZBar 库?

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:

你可以在 Python Azure 函数中如何使用 ZBar 库?

pyzbar got installed successfully as shown below:

你可以在 Python Azure 函数中如何使用 ZBar 库?

The function was deployed in Azure Portal, as shown below:

你可以在 Python Azure 函数中如何使用 ZBar 库?

Portal:

你可以在 Python Azure 函数中如何使用 ZBar 库?

Function:

你可以在 Python Azure 函数中如何使用 ZBar 库?

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

你可以在 Python Azure 函数中如何使用 ZBar 库?

I used the below commands to deploy my function app to azure from local env:-

func azure functionapp publish &lt;function-app-name&gt; 
func azure functionapp publish &lt;app_name&gt; --build remote

The remote build will include pyzbar package in the deployment

Output:-

你可以在 Python Azure 函数中如何使用 ZBar 库?

pyzbar got installed successfully like below:-

你可以在 Python Azure 函数中如何使用 ZBar 库?

Function got deployed in Azure Portal, Refer below:-

你可以在 Python Azure 函数中如何使用 ZBar 库?

Portal:-

你可以在 Python Azure 函数中如何使用 ZBar 库?

Function:-

你可以在 Python Azure 函数中如何使用 ZBar 库?

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 \
    &amp;&amp; apt-get install -y libzbar0

Build your docker image:-

docker build -t &lt;your_image_name&gt; .

Update your function app to use the docker image by referencing it in Host.json like below:-

{
  &quot;version&quot;: &quot;2.0&quot;,
  &quot;extensionBundle&quot;: {
    &quot;id&quot;: &quot;Microsoft.Azure.Functions.ExtensionBundle&quot;,
    &quot;version&quot;: &quot;[2.*, 3.0.0)&quot;
  },
  &quot;functions&quot;: [
    {
      &quot;name&quot;: &quot;MyFunction&quot;,
      &quot;docker&quot;: {
        &quot;image&quot;: &quot;&lt;your_image_name&gt;&quot;
      }
    }
  ]
}

> 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

huangapple
  • 本文由 发表于 2023年4月6日 19:14:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定