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
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

Incredible Demos

  • By
    Assign Anchor IDs Using MooTools 1.2

    One of my favorite uses of the MooTools JavaScript library is the SmoothScroll plugin. I use it on my website, my employer's website, and on many customer websites. The best part about the plugin is that it's so easy to implement. I recently ran...

  • By
    MooTools Zebra Tables Plugin

    Tabular data can oftentimes be boring, but it doesn't need to look that way! With a small MooTools class, I can make tabular data extremely easy to read by implementing "zebra" tables -- tables with alternating row background colors. The CSS The above CSS is extremely basic.

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!