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
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    Introducing MooTools ElementSpy

    One part of MooTools I love is the ease of implementing events within classes. Just add Events to your Implements array and you can fire events anywhere you want -- these events are extremely helpful. ScrollSpy and many other popular MooTools plugins would...

  • By
    CSS Selection Styling

    The goal of CSS is to allow styling of content and structure within a web page.  We all know that, right?  As CSS revisions arrive, we're provided more opportunity to control.  One of the little known styling option available within the browser is text selection styling.

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!