Heiko Finzel
Heiko Finzel

Reputation: 118

Ensure that a version of some package is newer than version x

is there a better way in python to ensure a script runs only with a version of a module newer than x as this?

import somemodule
assert int(somemodule.__version__[0]) > 1 # would enforce a version of at least 2.0

In perl one would do it like:

use somemodule 2.0

I would like to do this because I need a newer version than the one provided by Debian repositories and would like to ensure the user installed the lib via pip.

The point is, the script would run with the older package without errors but produce wrong outcome because of unfixed bugs in the old Debian module version.

PS: I need a solution that works for python2 (2.6/2.7) and python3.

Upvotes: 3

Views: 894

Answers (1)

be_good_do_good
be_good_do_good

Reputation: 4441

Why do you really have to convert to int. I know this is not pythonic way of doing it, but it definitely works

>>> assert('0.0.8' > '1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert('0.0.8' > '0')
>>> assert('0.0.8' > '0.0.7')
>>> assert('0.0.8' > '0.0.7.5')
>>> assert('0.0.8' > '0.0.7.5.8')
>>> assert('0.0.8' > '0.0.8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert('0.0.8' > '0.0.8.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>>

what I mean is assert(somemodule.__version__ > '1') # raise assertion error if version installed is less than 1

Upvotes: 1

Related Questions