Get a Python Package Version

By  on  

Part of maintaining a Django-based application like MDN's kuma is ensuring Python packages are up to date.  I was recently testing an upgrade on a remote system and needed to ensure that a given Python package was at the version number it should be.  Here's how I retrieved the package version:

import nose   # Nose is a test utility.  Replace with your desired package here.
nose.__version__

# Output:  0.3.1

The __version__ property returns the exact version number for a Python package.  Some Python packages use a VERSION property as well, but __version__ should be the most reliable.

Recent Features

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

Incredible Demos

  • By
    dwImageProtector Plugin for jQuery

    I've always been curious about the jQuery JavaScript library. jQuery has captured the hearts of web designers and developers everywhere and I've always wondered why. I've been told it's easy, which is probably why designers were so quick to adopt it NOT that designers...

  • By
    CSS Scoped Styles

    There are plenty of awesome new attributes we've gotten during the HTML5 revolution:  placeholder, download, hidden, and more.  Each of these attributes provides us a different level of control over an element on the page, but there's a new element attribute that allows...

Discussion

  1. Every once in a while a package might not define either VERSION or __version__, since they’re conventions, not required. You can always get the installed version with pkg_resources, though:

    >>> import pkg_resources
    >>> pkg_resources.get_distribution("PIL").version
    '1.1.7'
    
  2. Alfred Tarski

    @James, __version__ is recommended by PEP 396

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!