ImportError: Unable to find zbar shared library on Flask - python

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

In Ubuntu install zbar-tools
sudo apt-get install zbar-tools

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

In Ubuntu terminal simply run this command and this will install zbar in your global package
sudo apt-get install zbar-tools

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

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

Related

How can we use opencv in a multistage docker image?

I recently learned about the concept of building docker images based on a multi-staged Dockerfile.
I have been trying simple examples of multi-staged Dockerfiles, and they were working fine. However, when I tried implementing the concept for my own application, I was facing some issues.
My application is about object detection in videos, so I use python and Tensorflow.
Here is my Dockerfile:
FROM python:3-slim AS base
WORKDIR /objectDetector
COPY detect_objects.py .
COPY detector.py .
COPY requirements.txt .
ADD data /objectDetector/data/
ADD models /objectDetector/models/
RUN apt-get update && \
apt-get install protobuf-compiler -y && \
apt-get install ffmpeg libsm6 libxext6 -y && \
apt-get install gcc -y
RUN pip3 install update && python3 -m pip install --upgrade pip
RUN pip3 install tensorflow-cpu==2.9.1
RUN pip3 install opencv-python==4.6.0.66
RUN pip3 install opencv-contrib-python
WORKDIR /objectDetector/models/research
RUN protoc object_detection/protos/*.proto --python_out=.
RUN cp object_detection/packages/tf2/setup.py .
RUN python -m pip install .
RUN python object_detection/builders/model_builder_tf2_test.py
WORKDIR /objectDetector/models/research
RUN pip3 install wheel && pip3 wheel . --wheel-dir=./wheels
FROM python:3-slim
RUN pip3 install update && python3 -m pip install --upgrade pip
COPY --from=base /objectDetector /objectDetector
WORKDIR /objectDetector
RUN pip3 install --no-index --find-links=/objectDetector/models/research/wheels -r requirements.txt
When I try to run my application in the final stage of the container, I receive the following error:
root#3f062f9a5d64:/objectDetector# python detect_objects.py
Traceback (most recent call last):
File "/objectDetector/detect_objects.py", line 3, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
So per my understanding, it seems that opencv-python is not successfully moved from the 1st stage to the 2nd.
I have been searching around, and I found some good blogs and questions tackling the issue of multi-staging Dockerfiles, specifically for python libraries. However, it seems I missing something here.
Here are some references that I have been following to solve the issue:
How do I reduce a python (docker) image size using a multi-stage build?
Multi-stage build usage for cuda,cudnn,opencv and ffmpeg #806
So my question is: How can we use opencv in a multistage docker image?

aws eb opencv-python "web: from .cv2 import"

in aws-eb I am deployed an application -django- and there was no error on that process. Health is green and OK but page is giving Internal Server Error. so I checked the logs and saw the below error.
... web: from .cv2 import
... web: ImportError: libGL.so.1: cannot open shared object file: No such file or directory
while installing requirements.txt on deployment process opencv must be installed. because it includes opencv-python==4.5.5.64
so I not quite sure what is the above error pointing at.
and helpers.py this is how I am import it.
import requests
import cv2
libGL.so is installed with the package libgl1, pip3 install opencv-python is not sufficient here.
Connect the aws via ssh and run;
apt-get update && apt-get install libgl1
Or even better, consider using docker containers for the project and add the installation commands to the Dockerfile.
Also, as https://stackoverflow.com/a/66473309/12416058 suggests, Package python3-opencv includes all system dependencies of OpenCV. so installing it may prevent further errors.
To install python3-opencv;
apt-get update && apt-get install -y python3-opencv
pip install -r requirements.txt
To install in Dockerfile:
RUN apt-get update && apt-get install -y python3-opencv
RUN pip install -r requirements.txt

docker image build fails at RUN pip install -r requirements.txt in a Flask app

I have a Flask application with the following requirements.txt
chalice
matplotlib
sklearn
numpy
scipy
pandas
flask
flask_restful
and the following Dockerfile:
FROM python:3.6.1-alpine
WORKDIR /project
ADD . /project
RUN pip install -r requirements.txt
CMD ["python","app.py"]
Running the command docker image build -t clf_test .
generates the following error:
You are using pip version 9.0.1, however version 20.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1
It seems that the matplotlib can't get installed for some reason.
Running the pip install -r requirements.txt locally doesn't produce any errors
matplotlib must be built from source, and compiling it requires a number of supporting libraries as well as a functioning C compiler. You can figure out what these are and install them so that it builds properly...
...or you can just base your Dockerfile on the non-alpine python:3.6.1 image and then apt-get install python3-matplotlib before installing your other requirements. E.g., this builds without errors:
FROM python:3.6.1
WORKDIR /project
ADD . /project
RUN apt update; apt-get -y install python3-matplotlib
RUN pip install -r requirements.txt
CMD ["python","app.py"]

problem install pika (rabbitmq sdk in python ) in docker _ no module named 'pika'

I am trying to install rabbitmq (pika) driver in my python container, but in local deployment, there is no problem.
FROM ubuntu:20.04
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN apt-get update && apt-get -y install gcc python3.7 python3-pip
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
COPY . .
CMD ["python","index.py"]
this is my requerments.txt file :
requests
telethon
Flask
flask-mongoengine
Flask_JWT_Extended
Flask_Bcrypt
flask-restful
flask-cors
jsonschema
werkzeug
pandas
xlrd
Kanpai
pika
Flask-APScheduler
docker build steps complete with no error and install all the dependencies with no error but when I try to run my container it crashes with this error :
no module named 'pika'
installing python3.7 will not work here, you are still using python3.8 by using pip3 command and your CMD will also start python3.8, I suggest you to use python:3.7 base image
so try this:
FROM python:3.7
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN apt-get update && apt-get -y install gcc
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
CMD ["python","index.py"]

pip install error while building docker images

I'm using centos/python-36-centos7 as a base image of my application. In Dockerfile, after RUN pip install --upgrade pip, pip successfully upgrades from 9.0.1 to 18.0. Next step, at RUN pip install --no-cache-dir -r requirements.txt, docker keeps throwing error:
/bin/sh: /opt/app-root/bin/pip: /opt/app-root/bin/python3: bad interpreter: No such file or directory
The command '/bin/sh -c pip install --no-cache-dir -r requirements.txt' returned a non-zero code: 126
Operating systems: CentOS 7.2 64 bit
Docker version:18.06.0-ce, build 0ffa825
Complete Dockerfile:
FROM centos/python-36-centos7
MAINTAINER SamYu,sam_miaoyu#foxmail.com
USER root
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY . /faceDetectBaseImg
COPY ./pip.conf /etc/pip.conf
WORKDIR /faceDetectBaseImg
RUN yum install -y epel-release
RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
RUN rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
RUN rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
RUN yum install -y ffmpeg
RUN yum -y install libXrender
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
pip.conf:
[global]
trusted-host = mirrors.aliyun.com
index-url = https://mirrors.aliyun.com/pypi/simple
UPDATES:
problem fixed by removing pip install --upgrade pip and running pip 9.0.1. I am thinking it has something to do with pip 18.0 vs CentOS7 docker images. I would still like to know if there is a fix under pip 18.0.
Problems fixed completely by pulling centOS7 image and build python from source. As a reminder, don't use the latest version of centos/python-36-centos7 as of June 2018.

Categories

Resources