get python version,如果未设置__version__变量,如何检查python软件包的版本

I am using pyodbc and I want to know the version of it that I am using.

Apparently I cannot use pyodbc.__version__ because probably the variable is not set.

How else can I figure out the version of the package?

解决方案

It does have the version info, just use .version:

In [4]: pyodbc.version

Out[4]: '3.0.10'

The pip show command would also get it for you:

In [54]: pip.main(["show","pyodbc"])

---

Metadata-Version: 1.1

Name: pyodbc

Version: 3.0.10

Summary: DB API Module for ODBC

Home-page: http://code.google.com/p/pyodbc

Author: Michael Kleehammer

Author-email: michael@kleehammer.com

License: MIT

Location: /usr/local/lib/python2.7/dist-packages

Requires:

Classifiers:

Development Status :: 5 - Production/Stable

Intended Audience :: Developers

Intended Audience :: System Administrators

License :: OSI Approved :: MIT License

Operating System :: Microsoft :: Windows

Operating System :: POSIX

Programming Language :: Python

Programming Language :: Python :: 2

Programming Language :: Python :: 3

Topic :: Database

Out[54]: 0

You could redirect stdout and parse the output:

import pip

import sys

if sys.version_info.major >= 3:

from io import StringIO

else:

from StringIO import StringIO

def get_version(package):

f = StringIO()

sys.stdout = f

pip.main(["show", package])

sys.stdout = sys.__stdout__

return next((line.split(":", 1)[1].strip()

for line in f.getvalue().splitlines() if line.startswith("Version")), "No match")

But an easier way is to use pkg_resources, if you look at the source for show, you can see how it is gathered:

from pip._vendor import pkg_resources

def get_version(package):

package = package.lower()

return next((p.version for p in pkg_resources.working_set if p.project_name.lower() == package), "No match")

To use it just pass the package name:

In [57]: get_version("pyodbc")

Out[57]: '3.0.10'

In [58]: get_version("pandas")

Out[58]: '0.17.1'

In [59]: get_version("requests")

Out[59]: '2.9.1'

In [60]: get_version("foobar")

Out[60]: 'No match'

You can easily extend it to get different info using similar logic to the show command.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值