使用 python 的包时出现导入错误

import error when using packages of python

有人可以帮我解决这个错误吗?我正在使用 Ubuntu 20.04 和 jupyter notebook。 (我直接从 ubuntu 应用程序商店安装了项目 jupyter。我没有安装 anaconda 或 spyder。)

我试过关注但没有成功

!pip install opencv-python

sudo apt-get install python3-opencv

apt update && apt install -y libsm6 libxext6 libxrender-dev

奇怪的是,如果在其他 python 文件中写入相同的代码,并且通过终端 运行 它可以正常工作。但是我无法 运行 Jupyter Notebook

中的相同代码
ImportError                               Traceback (most recent call last)
<ipython-input-1-c8ec22b3e787> in <module>
----> 1 import cv2

/home/adarsh/snap/jupyter/common/lib/python3.7/site-packages/cv2/__init__.py in <module>
      3 import sys
      4 
----> 5 from .cv2 import *
      6 from .data import *
      7 

ImportError: libSM.so.6: cannot open shared object file: No such file or directory

你真的可以用 jupyter notebook 自己安装

conda install opencv

在我的代码前面添加以下行解决了我的问题

!pip install opencv-python-headless
# also contrib, if needed
!pip install opencv-contrib-python-headless