ImportError: Unable to find zbar shared library on Flask

ImportError: Unable to find zbar shared library on Flask

我正在尝试在 Docker

中的 Flask 服务器上使用 pyzbar 0.1.4

图像是我们创建的,基于从 alpine 获取的 python 2.7。

通过

安装ZBar
apk update
apk add zbar

我在 运行 dockerfile 时收到以下错误 File "/usr/lib/python2.7/site-packages/pyzbar/pyzbar.py", line 8, in <module> from .wrapper import ( File "/usr/lib/python2.7/site-packages/pyzbar/wrapper.py", line 166, in <module> c_uint_p, # minor File "/usr/lib/python2.7/site-packages/pyzbar/wrapper.py", line 159, in zbar_function return prototype((fname, load_libzbar())) File "/usr/lib/python2.7/site-packages/pyzbar/wrapper.py", line 135, in load_libzbar raise ImportError('Unable to find zbar shared library') ImportError: Unable to find zbar shared library

我正在尝试使用该库解码 QR 图像

Docker文件

FROM buffetcontainerimages.azurecr.io/base/buffetcloud-python:0.1
RUN pip install --upgrade pip setuptools wheel
COPY wheeldir /opt/app/wheeldir
COPY *requirements.txt /opt/app/src/
RUN pip install --use-wheel --no-index --find-links=/opt/app/wheeldir \
-r /opt/app/src/requirements.txt
RUN pip install --use-wheel --no-index --find-links=/opt/app/wheeldir \
-r /opt/app/src/test-requirements.txt
COPY . /opt/app/src/
WORKDIR /opt/app/src
RUN python setup.py install
EXPOSE 5000
CMD dronedemo

和requirements.txt

requests>=2.18.4
flask>=0.12.2
mechanize>=0.3.6
regex>=2.4.136
PyPDF2>=1.26.0
bs4>=4.5.3
pyzbar>=0.1.4
openpyxl>=2.5.0
selenium>=3.9.0
matplotlib>=2.1.2

当pip安装zbar时 ` pip 安装 zbar 收集 zbar 下载 zbar-0.10.tar.bz2 ... zbarmodule.h:26:18: 致命错误: zbar.h: 没有那个文件或目录

包括

编译终止。 错误:命令 'gcc' 失败,退出状态为 1 `

我很喜欢你的第二个表扬post,但你可能想尝试通过 pip 安装 pyzbar 依赖项。

FROM buffetcontainerimages.azurecr.io/base/buffetcloud-python:0.1
RUN pip install --upgrade pip setuptools wheel pyzbar
COPY wheeldir /opt/app/wheeldir
COPY *requirements.txt /opt/app/src/
RUN pip install --use-wheel --no-index --find-links=/opt/app/wheeldir \
-r /opt/app/src/requirements.txt
RUN pip install --use-wheel --no-index --find-links=/opt/app/wheeldir \
-r /opt/app/src/test-requirements.txt
RUN pip install -y pyzbar
COPY . /opt/app/src/
WORKDIR /opt/app/src
RUN python setup.py install
EXPOSE 5000
CMD dronedemo

一个简单的测试,看起来不错。

FROM python:2.7
RUN apt-get update && \
    apt-get install -y build-essential libzbar-dev && \
    pip install zbar

我试过 alpine.. 但是 zbar lib 仅在边缘分支中可用——试图让它工作比它值得的更麻烦。

PS。当心不在 docker 存储库中的图片。 -- 不知道这是你的图片


工作示例:

$ docker build -t yourimagenamehere .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM python:2.7
 ---> 9e92c8430ba0
... trunc...
Successfully built d951cd32ea74
Successfully tagged yourimagenamehere:latest
$ docker run -it --rm yourimagenamehere 
Python 2.7.14 (default, Dec 12 2017, 16:55:09) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zbar
>>> 

在Ubuntu安装zbar-tools

sudo apt-get install zbar-tools

Ubuntu终端中只需运行这个命令,这将在你的全局包中安装zbar

sudo apt-get install zbar-tools

我遇到了同样的问题(很高兴找到这个帖子)。不确定这是否已经解决,但这可能对您或未来的开发人员有所帮助。

像往常一样,它在我的本地机器上运行,但无法在容器中运行

我最初尝试的是:

  • 基于 Python3 图像构建图像

解决问题的原因:

  • 使用 FROM ubuntu:18.04
  • 构建
  • 在 Ubuntu 我能够安装 zbar 共享库。根据https://pypi.org/project/pyzbar/我们需要sudo apt-get install libzbar0
  • 设置 LC_ALL & LANG ENV 变量(不知道为什么,它是在另一个错误中提供的)
  • 在 requirements.txt 内将 Pillow==8.4.0 降级为 Pillow==6.2.2

我的 Dockerfile:

FROM ubuntu:18.04

RUN apt-get update -y
# Get's shared library for zbar
RUN apt-get install -y libzbar0
# Installs Python
RUN apt-get install -y python3-pip python3-dev build-essential

COPY . /app
WORKDIR /app
COPY requirements.txt .
RUN pip3 install -r requirements.txt

# Initially encountered an issue that indicated I had to set these ENVs
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]

和requirements.txt

fastapi==0.67.0
Pillow==6.2.2
pyzbar==0.1.8
urllib3==1.26.7
uvicorn==0.12.2