如何确定库的版本 [英] How to determine the version of a library

查看:69
本文介绍了如何确定库的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当库没有version属性时,如何从一段python代码中确定python库的版本?

How to determine the version of a python library, from a python piece of code, when the library has no version attribute?

这是最初没有 version 属性的 Pyvisa 库的示例.我已经完成了以下代码,当我无法确定它时,该代码返回未知版本.

Here is the example of the Pyvisa library which had no version attribute initially. I have done the following code that returns unknown for the version when I can't determine it.

try:
    import visa
    from visa import VisaIOError, Error
    pyvisa_version=0.
    try:
        pyvisa_version=float(visa.__version__)
    except:
        pass
    if pyvisa_version >0.:
        print 'pyvisa version: %f ' % pyvisa_version
    else:
        print 'pyvisa version: unknown'
    if pyvisa_version <= 1.5:
        try:
            from visa import vpp43
        except:
            print 'Cannot import vpp43.'
except:
    print 'Cannot import visa'

如果没有 version 属性,有没有人有更好的解决方案?

Does anyone has a better solution when there is no version attribute?

推荐答案

是的!这将为您提供系统上每个库的版本.

Yes! This will get you the versions of every library on your system.

pip freeze

或用于 python 3

or for python 3

pip3 freeze

它将向您运行它的终端输出一个列表.

It will output a list to the terminal you've run it in.

按照下面的评论:

import os
import glob
d = os.path.dirname('libname'.__file__)
dirs = glob.glob(d+'*')
for d in dirs:
if d.endswith('.dist-info'):
    d = d.split('/')
    f=d[-1]
    f = f.split('-')
    f = f[1:]
    f = ''.join(f)

和 'f[:-9]' 现在包含版本号.

and 'f[:-9]' now contains the version number.

这篇关于如何确定库的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆