下载
中文
注册

Python导入opencv模块报错,提示:ImportError: libGL.so.1: cannot open shared object file: No such file or directory

现象描述

Python导入opencv模块报错。操作及提示如下:

 ---------------------------------------------------------------------------
 ImportError                               Traceback (most recent call last)
 Cell In[1], line 1
 ----> 1 import cv2
       2 import numpy as np
       3 import ipywidgets as widgets

  File ~/miniconda3/lib/python3.9/site-packages/cv2/__init__.py:181
     176             if DEBUG: print("Extra Python code for", submodule, "is loaded")
     178     if DEBUG: print('OpenCV loader: DONE')
 --> 181 bootstrap()

 File ~/miniconda3/lib/python3.9/site-packages/cv2/__init__.py:153, in bootstrap()
     149 if DEBUG: print("Relink everything from native cv2 module to cv2 package")
     151 py_module = sys.modules.pop("cv2")
 --> 153 native_module = importlib.import_module("cv2")
     155 sys.modules["cv2"] = py_module
     156 setattr(py_module, "_native", native_module)

 File ~/miniconda3/lib/python3.9/importlib/__init__.py:127, in import_module(name, package)
     125             break
     126         level += 1
 --> 127 return _bootstrap._gcd_import(name[level:], package, level)
 ImportError: libGL.so.1: cannot open shared object file: No such file or directory 

解决方案

根据错误提示,环境中缺少“libGL.so.1”文件,即缺少GL库文件。执行如下操作安装安装GL库。

  • Ubuntu22.04版本镜像:
    # 更新apt-get
    apt-get update
    # 安装GL库
    apt install libgl1-mesa-glx
  • OpenEuler22.03版本镜像:
    # 安装GL库
    pip3 install opencv-python-headless