I would like to make sure that my tensorflow module is at least a certain version (for example, 1.13.2) inside my Python script. For me, it's not quite clear how to do this easily.
The following is one example I tried:
import tensorflow as tf
import re
match = re.match("(\d+)\.(\d+)\.(\d+).*", tf.__version__)
base = 100
num1 = match.group(1) * base ** 2 + match.group(2) * base + match.group(3)
num2 = 1 ** base ** 2 + 13 * base + 2 # Corresponds to 1.13.2
assert num1 >= num2, ("The required version is at least 1.13.2")
The above code has several problems.
1) If some version number is more than 100 (e.g. 1.12.801), it won't work.
2) The code itself is very ugly.
I know that the above is definitely not the way to go. Could you suggest any good ways?
I know this question is from last year, but I'd just like to provide the PEP compliant way for doing this.
from packaging import version
import tensorflow as tf
assert version.parse(tf.__version__) >= version.parse('1.13.2')
packaging
is a Python built-in module, and the more you can use built-in modules with your code, the better. 😃
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With