Ausrada404
Ausrada404

Reputation: 599

How to install python-opencv in amazon-sagemaker?

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)

Upvotes: 4

Views: 3648

Answers (4)

Hamzah Al-Qadasi
Hamzah Al-Qadasi

Reputation: 9786

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

Upvotes: 0

Amir Masoud Nourollah
Amir Masoud Nourollah

Reputation: 145

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

Upvotes: 2

Ausrada404
Ausrada404

Reputation: 599

This problem has been solved.

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

More details can be found in here.

Upvotes: 5

IvanPy
IvanPy

Reputation: 51

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.

Upvotes: 1

Related Questions