pythonopencvubuntu

Unable to import cv2 in Python Ubuntu environment


My Ubuntu version is :

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:    20.04
Codename:   focal

I tried to install various ways and they were successful always.

$sudo apt-get install python3-opencv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-opencv is already the newest version (4.2.0+dfsg-5).
0 upgraded, 0 newly installed, 0 to remove and 49 not upgraded.

But when I try to import, I get this result :

 import cv2, sys, numpy, os
      ImportError: No module named cv2

I don't have any clue what is wrong. I also tried to install using 'sudo pip install opencv-python', but every time I get the same result.


Solution

  • Have you tried using this solution from a bit of an older post?

    The post was: Stackoverflow Post which was based on this blog from 2015. The way this post outlines on fixing the issue is by manually compiling OpenCV yourself. (By my understanding). You could also try checking the official docs to compile with that: OpenCV Docs

    If this doesn't work however you could try just cloning the github repo (I know not the greatest way but it should work..)

    $ sudo apt-get install git
    $ git clone https://github.com/opencv/opencv.git