17

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?

LetsPlayYahtzee
  • 7,161
  • 12
  • 41
  • 65
  • Sometimes differently named version methods exist. After importing the package, you can use `dir()` to see which methods are available (like `.__version__`). Other common ones are `.version` and `.VERSION`. I think there is a PEP for this, but can't remember offhand and not every package adheres to it. – tatlar Jan 24 '19 at 19:05

4 Answers4

25

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.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
3

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.

AlexM
  • 1,020
  • 2
  • 17
  • 35
2

You can use this command (from terminal)

pip freeze | grep pyodbc

For installation of pip (debian, ubuntu):

apt-get install python-pip
qvpham
  • 1,896
  • 9
  • 17
  • And there was me thinking I had made a new discovery ;) – Padraic Cunningham Mar 29 '16 at 12:08
  • I am not sure which I answer I should accept, the seconds is more generic when it comes to linux users while the first solves the problem for everyone but it answers the question for the specific package only – LetsPlayYahtzee Mar 29 '16 at 12:51
  • the first answer of @Padraic is the best for your question. I didn't known it, because i don't use pyodbc. :) – qvpham Mar 29 '16 at 13:14
1

Go to start --> cmd --> pip list

It list's all the packages you installed with version

Ab_Fredo
  • 96
  • 6