import cv2 should not throw if libsm6, libxrender-dev, and libxext6 are not installed when using opencv-python-headless.
import cv2 throws.
Create the following Dockerfile:
FROM python:3.7-slim-stretch
# Print architecture
dpkg --print-architecture
# Install OpenCV's runtime dependencies
RUN apt-get update
RUN apt-get -y install libglib2.0-0
# Uncomment the unfollowing lines, to get it to work
# RUN apt-get -y install libsm6 \
# libxrender-dev \
# libxext6
# Install OpenCV headless using pip
RUN pip install opencv-python-headless==4.1.0.25
# Ensure that opencv and its dependencies
# are correctly installed
RUN python -c 'import cv2'
import cv2 will throw:sh
docker build --rm -t opencv-headless-exp:latest .
libsm6, libxrender-dev, and libxext6 and import cv2 won't throw anymore.Thanks for the report. I don't know why x11 is required, those builds are not linked with Qt. I'll have to investigate if I have missed some additional cmake flag which enables GUI functionality.
1. Create the following `Dockerfile`:
WFM in Ubuntu Bionic if I docker run -it python:3.7-slim-stretch bash and run the following specified commands by hand.
ldd also confirms that all the dependencies of cv2.*.so provided by the package are satisfied.
I noticed this happens if opencv-python gets accidentally pip installed.
I had no problem running this dockerfile:
FROM python:3.7-slim-stretch
# Print architecture
RUN dpkg --print-architecture
# Install OpenCV's runtime dependencies
RUN apt-get update
RUN apt-get -y install libglib2.0-0
# Install OpenCV headless using pip
RUN pip install opencv-python-headless==4.1.0.25
RUN python -c 'import cv2'
FROM python:3.6-slim-stretch works too.
Seems to work fine (no GUI deps needed):
docker run -it python:3.7-slim-stretch bash
root@41fb0dd9296b:/# apt-get update
Ign:1 http://deb.debian.org/debian stretch InRelease
Get:2 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:3 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Get:4 http://deb.debian.org/debian stretch Release [118 kB]
Get:5 http://deb.debian.org/debian stretch Release.gpg [2365 B]
Get:6 http://deb.debian.org/debian stretch-updates/main amd64 Packages [27.4 kB]
Get:7 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [503 kB]
Get:8 http://deb.debian.org/debian stretch/main amd64 Packages [7086 kB]
Fetched 7922 kB in 1s (4905 kB/s)
Reading package lists... Done
root@41fb0dd9296b:/# apt-get -y install libglib2.0-0
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libglib2.0-data libicu57 libxml2 sgml-base shared-mime-info xdg-user-dirs xml-core
Suggested packages:
sgml-base-doc debhelper
The following NEW packages will be installed:
libglib2.0-0 libglib2.0-data libicu57 libxml2 sgml-base shared-mime-info xdg-user-dirs xml-core
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 14.7 MB of archives.
After this operation, 53.1 MB of additional disk space will be used.
Get:1 http://deb.debian.org/debian stretch/main amd64 sgml-base all 1.29 [14.8 kB]
Get:2 http://deb.debian.org/debian stretch/main amd64 libicu57 amd64 57.1-6+deb9u3 [7705 kB]
Get:3 http://deb.debian.org/debian stretch/main amd64 libxml2 amd64 2.9.4+dfsg1-2.2+deb9u2 [920 kB]
Get:4 http://deb.debian.org/debian stretch/main amd64 libglib2.0-0 amd64 2.50.3-2+deb9u1 [2691 kB]
Get:5 http://deb.debian.org/debian stretch/main amd64 libglib2.0-data all 2.50.3-2+deb9u1 [2518 kB]
Get:6 http://deb.debian.org/debian stretch/main amd64 shared-mime-info amd64 1.8-1+deb9u1 [731 kB]
Get:7 http://deb.debian.org/debian stretch/main amd64 xdg-user-dirs amd64 0.15-2+b1 [52.2 kB]
Get:8 http://deb.debian.org/debian stretch/main amd64 xml-core all 0.17 [23.2 kB]
Fetched 14.7 MB in 0s (44.2 MB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package sgml-base.
(Reading database ... 6710 files and directories currently installed.)
Preparing to unpack .../0-sgml-base_1.29_all.deb ...
Unpacking sgml-base (1.29) ...
Selecting previously unselected package libicu57:amd64.
Preparing to unpack .../1-libicu57_57.1-6+deb9u3_amd64.deb ...
Unpacking libicu57:amd64 (57.1-6+deb9u3) ...
Selecting previously unselected package libxml2:amd64.
Preparing to unpack .../2-libxml2_2.9.4+dfsg1-2.2+deb9u2_amd64.deb ...
Unpacking libxml2:amd64 (2.9.4+dfsg1-2.2+deb9u2) ...
Selecting previously unselected package libglib2.0-0:amd64.
Preparing to unpack .../3-libglib2.0-0_2.50.3-2+deb9u1_amd64.deb ...
Unpacking libglib2.0-0:amd64 (2.50.3-2+deb9u1) ...
Selecting previously unselected package libglib2.0-data.
Preparing to unpack .../4-libglib2.0-data_2.50.3-2+deb9u1_all.deb ...
Unpacking libglib2.0-data (2.50.3-2+deb9u1) ...
Selecting previously unselected package shared-mime-info.
Preparing to unpack .../5-shared-mime-info_1.8-1+deb9u1_amd64.deb ...
Unpacking shared-mime-info (1.8-1+deb9u1) ...
Selecting previously unselected package xdg-user-dirs.
Preparing to unpack .../6-xdg-user-dirs_0.15-2+b1_amd64.deb ...
Unpacking xdg-user-dirs (0.15-2+b1) ...
Selecting previously unselected package xml-core.
Preparing to unpack .../7-xml-core_0.17_all.deb ...
Unpacking xml-core (0.17) ...
Setting up libglib2.0-0:amd64 (2.50.3-2+deb9u1) ...
No schema files found: doing nothing.
Setting up sgml-base (1.29) ...
Setting up libicu57:amd64 (57.1-6+deb9u3) ...
Setting up libxml2:amd64 (2.9.4+dfsg1-2.2+deb9u2) ...
Setting up libglib2.0-data (2.50.3-2+deb9u1) ...
Processing triggers for libc-bin (2.24-11+deb9u4) ...
Setting up shared-mime-info (1.8-1+deb9u1) ...
Setting up xml-core (0.17) ...
Setting up xdg-user-dirs (0.15-2+b1) ...
Processing triggers for sgml-base (1.29) ...
root@41fb0dd9296b:/# pip install opencv-python-headless==4.1.0.25
Collecting opencv-python-headless==4.1.0.25
Downloading https://files.pythonhosted.org/packages/29/b0/8fe2f63e85d102c92b8d5d36a784aaf38304693595774b5f8ac0deb462a2/opencv_python_headless-4.1.0.25-cp37-cp37m-manylinux1_x86_64.whl (20.0MB)
|鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 20.0MB 9.8MB/s
Collecting numpy>=1.14.5 (from opencv-python-headless==4.1.0.25)
Downloading https://files.pythonhosted.org/packages/ba/e0/46e2f0540370f2661b044647fa447fef2ecbcc8f7cdb4329ca2feb03fb23/numpy-1.17.2-cp37-cp37m-manylinux1_x86_64.whl (20.3MB)
|鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 20.3MB 12.6MB/s
Installing collected packages: numpy, opencv-python-headless
Successfully installed numpy-1.17.2 opencv-python-headless-4.1.0.25
root@41fb0dd9296b:/# python -c 'import cv2'
root@41fb0dd9296b:/# python
Python 3.7.4 (default, Sep 12 2019, 16:13:12)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__file__
'/usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so'
>>> exit()
root@41fb0dd9296b:/# ldd /usr/local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so
linux-vdso.so.1 (0x00007fffe6b1b000)
libz-a147dcb0.so.1.2.3 => /usr/local/lib/python3.7/site-packages/cv2/.libs/libz-a147dcb0.so.1.2.3 (0x00007f8645dbf000)
libavcodec-874f3d51.so.58.47.106 => /usr/local/lib/python3.7/site-packages/cv2/.libs/libavcodec-874f3d51.so.58.47.106 (0x00007f8644753000)
libavformat-fb41c63f.so.58.26.101 => /usr/local/lib/python3.7/site-packages/cv2/.libs/libavformat-fb41c63f.so.58.26.101 (0x00007f86442f6000)
libavutil-473e9eb1.so.56.26.100 => /usr/local/lib/python3.7/site-packages/cv2/.libs/libavutil-473e9eb1.so.56.26.100 (0x00007f864407b000)
libswscale-4e6f4703.so.5.4.100 => /usr/local/lib/python3.7/site-packages/cv2/.libs/libswscale-4e6f4703.so.5.4.100 (0x00007f8643df0000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f8643bee000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f86438da000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f86436d6000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f86434b9000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f86432b1000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f8642f2f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8642c2b000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8642a14000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8642675000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8647d49000)
libswresample-a49c020a.so.3.4.100 => /usr/local/lib/python3.7/site-packages/cv2/.libs/libswresample-a49c020a.so.3.4.100 (0x00007f8642457000)
libvpx-8459aeef.so.6.0.0 => /usr/local/lib/python3.7/site-packages/cv2/.libs/libvpx-8459aeef.so.6.0.0 (0x00007f8641f53000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f8641ce0000)
Still get the error when i add virtual environment ->
File "/venv/lib/python3.7/site-packages/cv2/__init__.py", line 3, in
from .cv2 import *
ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
Still get the error when i add virtual environment ->
File "/venv/lib/python3.7/site-packages/cv2/init.py", line 3, in
from .cv2 import *
ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
Install libgthread-2.0.so.0. It's a prerequisite for running manylinux1 wheels as per https://www.python.org/dev/peps/pep-0513/.
Most helpful comment
I noticed this happens if
opencv-pythongets accidentally pip installed.I had no problem running this dockerfile:
FROM python:3.6-slim-stretchworks too.