Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error trying to import cv2(opencv-python) package

I am trying to access my webcam with cv2(opencv-python) package.

When I try to import it I get this error:

Traceback (most recent call last):
  File "server.py", line 6, in <module>
    import cv2
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

Note: I am trying to import this package on putty, on Linode server - that might be useful information.

If anyone can explain to me what is happening and maybe solve the problem I will highly appreciate it!

like image 442
Marko Avatar asked Sep 20 '20 09:09

Marko


People also ask

How do I fix import cv2 error?

The Python "ModuleNotFoundError: No module named 'cv2'" occurs when we forget to install the opencv-python module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install opencv-python command.

How do I import a cv2 package?

Start the Python shell by typing python3 and then hit enter. You will be inside the Python shell where you can execute your Python code. Import the cv2 package which is the name of the OpenCV module. Type “import cv2” and hit enter.


Video Answer


1 Answers

Install opencv-python-headless instead of opencv-python. Server (headless) environments do not have GUI packages installed which is why you are seeing the error. opencv-python depends on Qt which in turn depends on X11 related libraries.

Other alternative is to run sudo apt-get install -y libgl1-mesa-dev which will provide the missing libGL.so.1 if you want to use opencv-python. The libgl1-mesa-dev package might be named differently depending on your GNU/Linux distribution.

Full installation guide for opencv-python can be found from the package documentation: https://github.com/skvark/opencv-python#installation-and-usage

like image 51
skvark Avatar answered Nov 01 '22 16:11

skvark