Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise an exception on the version number of a module

How can you raise an exception when you import a module that is less or greater than a given value for its __version__?

There are a lot of different ways you could do it, but I feel like there must be some really simple way that eludes me at the moment. In this case the version number is of the format x.x.x

like image 589
ʞɔıu Avatar asked Mar 01 '23 02:03

ʞɔıu


1 Answers

Python comes with this inbuilt as part of distutils. The module is called distutils.version and is able to compare several different version number formats.

from distutils.version import StrictVersion

print StrictVersion('1.2.2') > StrictVersion('1.2.1')

For way more information than you need, see the documentation:

>>> import distutils.version
>>> help(distutils.version)
like image 179
gak Avatar answered Mar 03 '23 15:03

gak