python中opencv安装_我无法在docker中安装opencv-contrib-python

小编典典

我的猜测是,-alpine由于opencv软件包是二进制发行版(不仅是Python代码),而且版本不是在Alpine上构建的,因此您会看到该版本的失败。Alpine使用的C库与其他所有库都不相同(Alpine使用MUSL

libc,而其他几乎所有的库都使用Glibc);opencv代码库甚至可能无法为MUSL构建。也许仅仅是没有人去构建二进制包。无论哪种情况,最好使用以下选项之一:

如果我使用普通的python:3.5图像(而不是Alpine图像),则可以正常工作:

$ docker run -it --rm python:3.5 bash

root@95c81040aeaf:/# pip install opencv-contrib-python-headless

Collecting opencv-contrib-python-headless

Downloading https://files.pythonhosted.org/packages/c2/50/2427b286652cf64ea3618d08bfba38c04b6571f6f2c054e950367a2f309f/opencv_contrib_python_headless-3.4.3.18-cp35-cp35m-manylinux1_x86_64.whl (24.0MB)

100% |████████████████████████████████| 24.1MB 2.4MB/s

Collecting numpy>=1.11.1 (from opencv-contrib-python-headless)

Downloading https://files.pythonhosted.org/packages/86/04/bd774106ae0ae1ada68c67efe89f1a16b2aa373cc2db15d974002a9f136d/numpy-1.15.4-cp35-cp35m-manylinux1_x86_64.whl (13.8MB)

100% |████████████████████████████████| 13.8MB 4.7MB/s

Installing collected packages: numpy, opencv-contrib-python-headless

Successfully installed numpy-1.15.4 opencv-contrib-python-headless-3.4.3.18

root@95c81040aeaf:/# python

Python 3.5.6 (default, Nov 16 2018, 22:45:03)

[GCC 6.3.0 20170516] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import cv2

>>>

如果使用3.5-slim标记,则会看到与您报告的错误相同的错误:

root@63dca11a527f:/# python

Python 3.5.5 (default, May 5 2018, 03:17:29)

[GCC 4.9.2] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import cv2

Traceback (most recent call last):

File "", line 1, in

File "/usr/local/lib/python3.5/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

>>>

从包查询中可以看到,该库归libglib2.0包所有,-slim在Python映像版本中默认未安装该库。我们可以解决此问题:

# apt-get update

# apt-get -y install libglib2.0

现在,它按预期运行:

root@63dca11a527f:/# python

Python 3.5.5 (default, May 5 2018, 03:17:29)

[GCC 4.9.2] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import cv2

>>>

您可以使用以下方法结合此修复程序来构建自己的图像 Dockerfile:

FROM python:3.5-slim

RUN apt-get update && apt-get -y install libglib2.0; apt-get clean

RUN pip install opencv-contrib-python-headless

更新资料

关于您的评论:如果您希望软件包可用于在容器中运行的代码,那么,是的,您必须安装它。它还会从哪里来?

如果opencv-contrib-python-headless您的中包含 requirements.txt,则评论中发布的内容应该可以正常工作:

FROM python:3.5

COPY . /app

WORKDIR /app

RUN pip3 install -r requirements.txt

ENTRYPOINT ["python3"]

CMD ["app.py"]

如果requirements.txt不包括(为什么不包括),则需要显式安装:

FROM python:3.5

RUN pip install opencv-contrib-python-headless

COPY . /app

WORKDIR /app

RUN pip3 install -r requirements.txt

ENTRYPOINT ["python3"]

CMD ["app.py"]

2020-06-17

你可能感兴趣的:(python中opencv安装)