Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check version of Python library that doesn't define __version__

Tags:

python

I'm dealing with a Python library that does not define the __version__ variable (sqlalchemy-migrate), and I want to have different behavior in my code based on what version of the library I have installed.

Is there a way to check at runtime what version of the library is installed (other than, say, checking the output of pip freeze)?

like image 404
Lorin Hochstein Avatar asked May 08 '26 08:05

Lorin Hochstein


1 Answers

This being Python, the accepted way of doing this is generally to call something in the library that behaves differently depending on the version you have installed, something like:

import somelibrary
try:
    somelibrary.this_only_exists_in_11()
    SOME_LIBRARY_VERSION = 1.1
except AttributeError:
    SOME_LIBRARY_VERSION = 1.0

A more elegant way might be to create wrapper functions.

def call_11_feature():
    try:
        somelibrary.this_only_exists_in_11()
    except AttributeError:
        somelibrary.some_convoluted_methods()
        somelibrary.which_mimic()
        somelibrary.the_11_feature()
like image 183
Chris B. Avatar answered May 10 '26 22:05

Chris B.