我试图运行cv2,但当我试图导入它时,我得到以下错误:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

建议的在线解决方案是安装

apt install libgl1-mesa-glx

但这是已经安装的最新版本。

注:我实际上是在Docker上运行这个,我无法检查OpenCV版本。我尝试导入matplotlib,导入正常。


当前回答

我在docker容器中使用cv2时遇到了这个问题。我通过:

pip install opencv-contrib-python

安装opencv-contrib-python而不是opencv-python。

其他回答

在我看来,这是一个更好的解决方案。python3-opencv包包含OpenCV的所有系统依赖项。

RUN apt-get update && apt-get install -y python3-opencv
RUN pip install opencv-python

我在docker容器中使用cv2时遇到了这个问题。我通过:

pip install opencv-contrib-python

安装opencv-contrib-python而不是opencv-python。

把这个放到Dockerfile中

RUN apt-get update
RUN apt install -y libgl1-mesa-glx

排队前

COPY requirements.txt requirements.txt

例如

......

RUN apt-get update
RUN apt install -y libgl1-mesa-glx

COPY requirements.txt requirements.txt

......

在我的情况下,做以下就足够了,与上面的解决方案相比,这也节省了空间

RUN apt-get update && apt-get install -y --no-install-recommends \
        libgl1 \
        libglib2.0-0 \

当我试图在GCP Appengine Flex服务器环境中使用OpenCV时,我得到了同样的错误。将requirements.txt中的“opencv-python”替换为“opencv-python-headless”解决了这个问题。

OpenCV文档讨论了桌面和服务器(无头)环境的不同包。