ImportError: Unable to find zbar shared library on Flask

Question:

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
Asked By: Nicolas Fuchs

||

Answers:

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
Answered By: Ivonet

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
>>> 
Answered By: Javier Buzzi

In Ubuntu install zbar-tools

sudo apt-get install zbar-tools
Answered By: Mise

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

sudo apt-get install zbar-tools
Answered By: Kautuk Dwivedi

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
Answered By: Stefan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.