Checking a Python module version at runtime

Many third-party Python modules have an attribute which holds the version information for the module (usually something like module.VERSION or module.__version__), however some do not.

Particular examples of such modules are libxslt and libxml2.

I need to check that the correct version of these modules are being used at runtime. Is there a way to do this?

A potential solution wold be to read in the source at runtime, hash it, and then compare it to the hash of the known version, but that's nasty.

Is there a better solutions?


I'd stay away from hashing. The version of libxslt being used might contain some type of patch that doesn't effect your use of it.

As an alternative, I'd like to suggest that you don't check at run time (don't know if that's a hard requirement or not). For the python stuff I write that has external dependencies (3rd party libraries), I write a script that users can run to check their python install to see if the appropriate versions of modules are installed.

For the modules that don't have a defined 'version' attribute, you can inspect the interfaces it contains (classes and methods) and see if they match the interface they expect. Then in the actual code that you're working on, assume that the 3rd party modules have the interface you expect.

Use pkg_resources. Anything installed from PyPI at least should have a version number.

>>> import pkg_resources
>>> pkg_resources.get_distribution("blogofile").version

Some ideas:

  1. Try checking for functions that exist or don't exist in your needed versions.
  2. If there are no function differences, inspect function arguments and signatures.
  3. If you can't figure it out from function signatures, set up some stub calls at import time and check their behavior.

You can use

pip freeze

to see the installed packages in requirements format.

You can use importlib_metadata library for this.

If you're on python < 3.8, first install it with:

pip install importlib_metadata

Since python 3.8 it's included in python's standard library.

Then, to check a package's version (in this example lxml) run:

>>> from importlib_metadata import version
>>> version('lxml')

Keep in mind that this works only for packages installed from PyPI. Also, you must pass a package name as an argument to the version method, rather than a module name that this package provides (although they're usually the same).

I found it quite unreliable to use the various tools available (including the best one pkg_resources mentioned by this other answer), as most of them do not cover all cases. For example

  • built-in modules
  • modules not installed but just added to the python path (by your IDE for example)
  • two versions of the same module available (one in python path superseding the one installed)

Since we needed a reliable way to get the version of any package, module or submodule, I ended up writing getversion. It is quite simple to use:

from getversion import get_module_version
import foo
version, details = get_module_version(foo)

See the documentation for details.

Need Your Help

Why can other methods be "static" but a constructor cannot?

java static constructor

I do not understand why the main method has to be static. I understand static variables but static methods are difficult for me to grasp. Do static method exists so that one can create two methods ...

switch audio source with jquery and HTML5 audio tag

jquery html5 audio html5-audio

I have only found one other solution but it was incomplete so I need help here.