我无法使用 opencv2 并收到 ImportError: libgl.so.1 cannot open shared object file no such file or directory

Posted

技术标签:

【中文标题】我无法使用 opencv2 并收到 ImportError: libgl.so.1 cannot open shared object file no such file or directory【英文标题】:I cannot use opencv2 and received ImportError: libgl.so.1 cannot open shared object file no such file or directory 【发布时间】:2021-02-16 04:54:47 【问题描述】:

**env:**ubuntu16.04 anaconda3 python3.7.8 cuda10.0 gcc5.5

命令:

conda activate myenv
python
import cv2

错误: 回溯(最近一次通话最后): 文件“”,第 1 行,在 文件“/home/.conda/envs/myenv/lib/python3.7/site-packages/cv2/__init__.py”,第 5 行,在 从 .cv2 导入 * ImportError:libGL.so.1:无法打开共享对象文件:没有这样的文件或目录

我试过了:

RUN apt install libgl1-mesa-glx -y
    RUN apt-get install 'ffmpeg'\
        'libsm6'\
        'libxext6'  -y

但这已经安装并且是最新版本(libgl1-mesa-glx18.0.5-0ubuntu0~16.04.1)。 然后我尝试了:

sudo apt-get install --reinstall libgl1-mesa-glx

它不起作用。 最后,我尝试删除包:

sudo apt-get --purge remove libgl1-mesa-glx

发生另一个错误:

Reading package list... Done
Analyzing the dependency tree of the package
Reading status information... Done
Some packages cannot be installed. If you are using an unstable distribution, this may be
Because the system cannot reach the state you requested. There may be some software you need in this version
The packages have not been created yet or they have been moved out of the Incoming directory.
The following information may be helpful in solving the problem:

The following packages have unmet dependencies:
 libqt5multimedia5-plugins: Dependency: libqgsttools-p1 (>= 5.5.1) but it will not be installed
E: Error, pkgProblemResolver::Resolve failed. This may be due to a software package being required to maintain the status quo.

任何帮助都会非常有帮助。在此先感谢。

conda 列表:

# packages in environment at /home/lwy/.conda/envs/mmdet1:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
_openmp_mutex             4.5                       1_gnu    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
addict                    2.3.0                     <pip>
albumentations            0.5.1                     <pip>
appdirs                   1.4.4                     <pip>
asynctest                 0.13.0                    <pip>
attrs                     20.2.0                    <pip>
ca-certificates           2020.6.20            hecda079_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
certifi                   2020.6.20        py37he5f6b98_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
chardet                   3.0.4                     <pip>
cityscapesScripts         2.1.7                     <pip>
codecov                   2.1.10                    <pip>
coloredlogs               14.0                      <pip>
coverage                  5.3                       <pip>
cycler                    0.10.0                    <pip>
Cython                    0.29.21                   <pip>
decorator                 4.4.2                     <pip>
flake8                    3.8.4                     <pip>
future                    0.18.2                    <pip>
humanfriendly             8.2                       <pip>
idna                      2.10                      <pip>
imagecorruptions          1.1.0                     <pip>
imageio                   2.9.0                     <pip>
imgaug                    0.4.0                     <pip>
importlib-metadata        2.0.0                     <pip>
iniconfig                 1.1.1                     <pip>
isort                     5.6.4                     <pip>
kiwisolver                1.3.1                     <pip>
kwarray                   0.5.10                    <pip>
ld_impl_linux-64          2.35                 h769bd43_9    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libffi                    3.2.1                         1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
libgcc-ng                 9.3.0               h5dbcf3e_17    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libgomp                   9.3.0               h5dbcf3e_17    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libstdcxx-ng              9.3.0               h2ae2ef3_17    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
matplotlib                3.3.2                     <pip>
mccabe                    0.6.1                     <pip>
mmcv                      1.1.6                     <pip>
mmdet                     1.2.0+unknown             <pip>
ncurses                   6.2                  he1b5a44_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
networkx                  2.5                       <pip>
numpy                     1.19.4                    <pip>
opencv-python             4.4.0.46                  <pip>
openssl                   1.1.1h               h516909a_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ordered-set               4.0.2                     <pip>
packaging                 20.4                      <pip>
Pillow                    6.2.2                     <pip>
pip                       20.2.4                     py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pluggy                    0.13.1                    <pip>
py                        1.9.0                     <pip>
pycocotools               2.0                       <pip>
pycodestyle               2.6.0                     <pip>
pyflakes                  2.2.0                     <pip>
pyparsing                 2.4.7                     <pip>
pyquaternion              0.9.9                     <pip>
pytest                    6.1.2                     <pip>
pytest-cov                2.10.1                    <pip>
pytest-runner             5.2                       <pip>
python                    3.7.8           h6f2ec95_1_cpython    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python-dateutil           2.8.1                     <pip>
python_abi                3.7                     1_cp37m    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
PyWavelets                1.1.1                     <pip>
PyYAML                    5.3.1                     <pip>
readline                  8.0                  he28a2e2_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
requests                  2.24.0                    <pip>
scikit-image              0.17.2                    <pip>
scipy                     1.5.3                     <pip>
setuptools                49.6.0           py37he5f6b98_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
Shapely                   1.7.1                     <pip>
six                       1.15.0                    <pip>
sqlite                    3.33.0               h4cf870e_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tifffile                  2020.10.1                 <pip>
tk                        8.6.10               hed695b0_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
toml                      0.10.2                    <pip>
torch                     1.5.0+cu92                <pip>
torchvision               0.6.0+cu92                <pip>
tqdm                      4.51.0                    <pip>
typing                    3.7.4.3                   <pip>
ubelt                     0.9.3                     <pip>
urllib3                   1.25.11                   <pip>
wheel                     0.35.1             pyh9f0ad1d_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
xdoctest                  0.15.0                    <pip>
xz                        5.2.5                h516909a_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
yapf                      0.30.0                    <pip>
zipp                      3.4.0                     <pip>
zlib                      1.2.11                        0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free

【问题讨论】:

【参考方案1】:

通常,这些 Pacakge 旨在作为系统包安装,而不仅仅是 Python 包。因此,即使在成功安装 opencvcmakedlib 等软件包后,它们也无法正常工作。

安装它们的最佳方法是使用。

sudo apt-get install python3-opencv

这是官方Opencv Docs.在Ubuntu上成功安装opencv的首选方法

【讨论】:

【参考方案2】:

我已经解决了这个问题! 首先,找到文件:

find /usr -name libgl*

我找到了 /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 然后,我创建了一个软链接:

ln -s /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 /usr/lib/libGL.so.1

最后,我验证了它是有效的:

python
import cv2

【讨论】:

以上是关于我无法使用 opencv2 并收到 ImportError: libgl.so.1 cannot open shared object file no such file or directory的主要内容,如果未能解决你的问题,请参考以下文章

请帮我找出错误

无法使用 opencv2 打开视频

无法使用 OpenCV2 检测面部标志

无法使用 opencv2.3 imread 读取图像

在 Android Studio 中找不到 opencv2

无法将 opencv2-2 框架添加到 iOS 项目