How to check version of python package if no __version__ variable is set
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: [email protected]
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.
To get a short report about the module, including its version, run:
pip show pyodbc
in a command prompt (e.g. Windows cmd
). This works even for modules without a __version__ attribute.