10

Im trying to use pyzbar 0.1.4 on a Flask Server in Docker

The image was created by us, based in python 2.7 taken from alpine.

Install ZBar by

apk update
apk add zbar

Im getting the following error when running 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

Im trying to decode a QR image using that library

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

And 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

When pip install zbar

pip install zbar
Collecting zbar
Downloading zbar-0.10.tar.bz2
...
zbarmodule.h:26:18: fatal error: zbar.h: No such file or directory
#include <zbar.h>
compilation terminated.
error: command 'gcc' failed with exit status 1
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Nicolas Fuchs
  • 125
  • 1
  • 1
  • 13
  • Can you share the Dockerfile and stuff? – Ivonet Feb 14 '18 at 17:39
  • 2
    This looks strange. dll usually means windows. then youre showing a stack trace with '/usr/lib/python2.7/...' which tells me youre on a *nix system.. can you please post your dockerfile (a must) and python req file just in case. – Javier Buzzi Feb 14 '18 at 17:40
  • @Ivonet Added dockerfile and requirements – Nicolas Fuchs Feb 14 '18 at 18:00
  • Where is this image coming from? What is it based of? `scratch`? `debian`? how is the package installed? – Javier Buzzi Feb 14 '18 at 18:31
  • On another note: pyzbar looks like its a wrapper FOR windows, https://github.com/NaturalHistoryMuseum/pyzbar/ so this is not what you want to use. – Javier Buzzi Feb 14 '18 at 18:34
  • @JavierBuzzi The image was created by us, based in python 2.7 taken from alpine. The package is installed by apk update apk add zbar – Nicolas Fuchs Feb 14 '18 at 19:21
  • At any rate, i _think_ `pyzbar` (they have a wheel for "any" environment -- but something tells me they didnt test it) is completely out of the question since it wants to be installed on windows, try just pip installing `zbar` – Javier Buzzi Feb 14 '18 at 19:26

5 Answers5

17

In Ubuntu install zbar-tools

sudo apt-get install zbar-tools
Mise
  • 3,267
  • 1
  • 22
  • 22
7

A simple test, looks good.

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

i tried alpine.. but the zbar lib is only available in the edge branch -- trying to get it to work was more trouble than it was worth.

PS. beware of images that are not in the docker repo. -- didnt know it was your image


Working example:

$ 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
>>> 
Javier Buzzi
  • 6,296
  • 36
  • 50
  • Where you get the image Python:2.7 ? Alpine – Nicolas Fuchs Feb 14 '18 at 19:45
  • 1
    @NicolasFuchs from the official docker repo (https://hub.docker.com/_/python/) if you run this, it will automagically download it for you and built the image for you -- you dont need to have it, docker is smart enough to go and fetch it. Pay attention to the *Working example* and look for the `$` and you can do this yourself – Javier Buzzi Feb 14 '18 at 19:57
2

In Ubuntu terminal simply run this command and this will install zbar in your global package

sudo apt-get install zbar-tools
Ne1zvestnyj
  • 1,391
  • 1
  • 7
  • 25
Kautuk Dwivedi
  • 299
  • 2
  • 2
1

I encountered the same issue (happy to have found this thread). Not sure if this has already been solved but this might help you or future devs.

As usual, it worked on my machine locally but couldn't get it to work in a container

What I tried initially:

  • Building an image based on a Python3 image

What solved the issue:

  • Build with FROM ubuntu:18.04
  • Within Ubuntu I was able to install the zbar shared library. According to https://pypi.org/project/pyzbar/ we need sudo apt-get install libzbar0
  • Set LC_ALL & LANG ENV variables (not sure why, it was provided in an additional error)
  • Within requirements.txt downgrade Pillow==8.4.0 to Pillow==6.2.2

My 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"]

And requirements.txt

fastapi==0.67.0
Pillow==6.2.2
pyzbar==0.1.8
urllib3==1.26.7
uvicorn==0.12.2
Stefan
  • 23
  • 3
0

I grea with the second commend on your post, but you might want to try to install the pyzbar dependency through pip.

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
Ivonet
  • 2,492
  • 2
  • 15
  • 28