Checking version of OpenCV (cv)
Asked Answered
S

4

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?

Spring answered 19/10, 2017 at 19:40 Comment(0)
T
26

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

Tumbrel answered 19/10, 2017 at 20:32 Comment(0)
A
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))
Afoot answered 19/10, 2017 at 20:59 Comment(0)
C
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]
Crestfallen answered 28/2, 2019 at 15:50 Comment(0)
C
0

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

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

Cotsen answered 9/1, 2023 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.