4

I installed the OpenCV package by pip install opencv-python.

When I import cv2 in my code.

import cv2

I got the following error:


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/tmp/ipykernel_324/571303353.py in <module>
----> 1 import cv2

~/.conda/envs/default/lib/python3.9/site-packages/cv2/__init__.py in <module>
      6 import sys
      7 
----> 8 from .cv2 import *
      9 from .cv2 import _registerMatType
     10 from . import mat_wrapper

ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

Here are some solutions for this error but seemly need root permission.

apt-get update -y
apt-get install libglib2.0-0

When I run these commands in the terminal I got the following errors.

(studiolab) studio-lab-user@default:~/sagemaker-studiolab-notebooks/vit/ViT-pytorch$ apt-get update -y
Reading package lists... Done
E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission denied)

4 Answers 4

5

This problem has been solved.

pip uninstall opencv-python
pip install opencv-python-headless

More details can be found in here.

Sign up to request clarification or add additional context in comments.

Comments

2

I had the same issue. With this line, you can install the glib dependency for Amazon Sagemaker Studio Lab. Just run it on your notebook cell.

! conda install glib=2.51.0 -y

You also can create another virtual environment for your session that contains glib:

! conda create -n glib-test -c defaults -c conda-forge python=3 glib=2.51.0` -y

Comments

1

I faced this ImportError too and Thanks to @AmirMasoud's suggested solution. After running ! conda install glib=2.51.0 -y in my virtual environment, now cv2 is imported successfully.

Comments

0

You should follow this order:

!pip uninstall -y opencv-python
!conda install glib=2.51.0 -y
!pip install opencv-python
!pip install opencv-python-headless

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.