【问题标题】:ImportError: Unable to find zbar shared library on FlaskImportError:无法在 Flask 上找到 zbar 共享库
【发布时间】:2022-01-16 20:09:34
【问题描述】:

我试图在 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 图像

Dockerfile

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 ` 点安装zbar 收集 zbar 下载 zbar-0.10.tar.bz2 ... zbarmodule.h:26:18:致命错误:zbar.h:没有这样的文件或目录

包括

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

【问题讨论】:

  • 你能分享一下 Dockerfile 之类的吗?
  • 这看起来很奇怪。 dll 通常表示 windows。然后你用'/usr/lib/python2.7/...'显示一个堆栈跟踪,它告诉我你在一个*nix系统上..你能否发布你的dockerfile(必须)和python req文件以防万一。
  • @Ivonet 添加了 dockerfile 和要求
  • 这张图片来自哪里?它的依据是什么? scratch? debian?包是怎么安装的?
  • 另一方面:pyzbar 看起来像是 Windows 的包装器,github.com/NaturalHistoryMuseum/pyzbar 所以这不是你想要使用的。

标签: python zbar


【解决方案1】:

在 Ubuntu 中安装 zbar-tools

sudo apt-get install zbar-tools

【讨论】:

    【解决方案2】:

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

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

    我尝试了 alpine.. 但 zbar 库仅在边缘分支中可用 - 试图让它工作比它值得的麻烦更多。

    PS。注意不在 docker repo 中的图像。 -- 不知道这是你的图像


    工作示例:

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

    【讨论】:

    • 从哪里得到图像 Python:2.7 ?高山
    • @NicolasFuchs 来自官方 docker repo (hub.docker.com/_/python) 如果你运行它,它会自动为你下载它并为你构建镜像——你不需要它,docker 很聪明足以去取它。注意Working example并寻找$,你可以自己做
    【解决方案3】:

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

    sudo apt-get install zbar-tools
    

    【讨论】:

      【解决方案4】:

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

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

      我最初的尝试:

      • 基于 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
      

      【讨论】:

        【解决方案5】:

        我很喜欢你帖子中的第二个推荐,但你可能想尝试通过 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
        

        【讨论】:

          猜你喜欢
          • 2023-02-09
          • 1970-01-01
          • 2023-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-30
          • 1970-01-01
          相关资源
          最近更新 更多