Python 检查 OpenCV (cv) 的版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46837658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:52:19  来源:igfitidea点击:

Checking version of OpenCV (cv)

pythonopencvpython-importimporterror

提问by Venkat kamal

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

我正在检查系统中先前安装的 OpenCV 版本。我试图检查使用

from cv2 import __version__

Its gave me the following error

它给了我以下错误

No module named cv2

没有名为 cv2 的模块

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

当我尝试时import cv,它没有给我错误。有没有办法知道版本?

回答by Giordano

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

打开一个python解释器(只需在终端中输入python)。
现在,您应该导入cv2然后检查特殊变量version
像这样:

import cv2
cv2.__version__

For more details, check this link

有关更多详细信息,请查看此链接

回答by charlesreid1

__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):

__version__是一个变量和包的属性,而不是你可以导入的东西。执行此操作的一般方法(来自脚本或解释器,Python 2 或 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:

您可以使用__version__字符串以这种方式检查任何 Python 包的版本号。另请注意,如果您想知道还有哪些特殊__variables__功能可用,您可以使用dir()模块上的函数:

import cv2
print(dir(cv2))

回答by Oliver Zendel

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:

我有一个没有cv2.__version__变量的旧设置(鸡蛋文件用于在 Windows 10 下为 python 2.7.6 安装 opencv 2.4.2;刚刚返回AttributeError: 'module' object has no attribute '__version__')这适用于所有设置:

opencv_version = cv2.__version__ if hasattr(cv2, '__version__') else cv2.__file__.replace('\','').replace('/','').split("cv2-")[-1].split("-")[0]