Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether the current tensorflow version is at least a specific version in a python script?

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?

like image 469
chanwcom Avatar asked Oct 19 '25 00:10

chanwcom


1 Answers

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. 😃

like image 169
Aswin Sivaraman Avatar answered Oct 20 '25 14:10

Aswin Sivaraman