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
Checking version of OpenCV (cv)
提问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
回答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]