13

I was checking the version of OpenCV installed previously in a system. I tried to check using

from cv2 import __version__

Its gave me the following error

No module named cv2

When I tried import cv, it's not giving me error. Is there a way to know the version?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Venkat kamal
  • 279
  • 2
  • 3
  • 8

4 Answers4

24

Open a python interpreter (simply type python in your terminal).
Now, you should import cv2 and then check the special variable version.
Like this:

import cv2
cv2.__version__

For more details, check this link

Giordano
  • 5,422
  • 3
  • 33
  • 49
6

__version__ is a variable and a property of the package, not something you can import. The general way to do this (from script or interpreter, Python 2 or Python 3):

import cv2
print(cv2.__version__)

You can check the version number of any Python package this way using the __version__ string. Also note that if you want to know what other special __variables__ are available, you can use the dir() function on your module:

import cv2
print(dir(cv2))
charlesreid1
  • 4,360
  • 4
  • 30
  • 52
2

I had an old setup without a cv2.__version__ variable (egg file was used to install opencv 2.4.2 for python 2.7.6 under windows 10; just returned AttributeError: 'module' object has no attribute '__version__') This worked for all setups:

opencv_version = cv2.__version__ if hasattr(cv2, '__version__') else cv2.__file__.replace('\\','').replace('/','').split("cv2-")[-1].split("-")[0]
Oliver Zendel
  • 2,695
  • 34
  • 29
0

def print_hi(version): print(f'Cv2 version is : {version}')

if name == 'main': import cv2 print_hi(cv2.version)

Pratap
  • 9
  • 2