cdahms
cdahms

Reputation: 3750

checking TensorFlow version in Python - "tf.__version__" vs "tf.VERSION"?

Shortest possible version of question:

When checking TensorFlow version in Python, can somebody explain/clarify the difference between tf.__version__ and tf.VERSION ??

Slightly longer version of question:

This seems to work:

if tf.__version__ < "1.8.0":
    print("ERROR: currently running TensorFlow version " + tf.__version__ + ", at least version 1.8.0 is required")
    return
# end if

This also seems to work:

if tf.VERSION < "1.8.0":
    print("ERROR: currently running TensorFlow version " + tf.VERSION + ", at least version 1.8.0 is required")
    return
# end if

One difference I have noticed is that the editor PyCharm shows a warning for the tf.__version__ way but does not for the tf.VERSION way:

enter image description here

A work around for this in PyCharm is to add the # noinspection PyUnresolvedReferences comment above each tf.__version__ useage, which resolved the warning:

enter image description here

However the tf.VERSION way does not show a warning in PyCharm without the # noinspection PyUnresolvedReferences comment:

enter image description here

I asked about the PyCharm tf.__version__ warning in this post:

TensorFlow Python warning in PyCharm - Cannot find reference __version__ in __init__.py

and the only responder stated that tf.__version__ is generated dynamically. In the context of Python specifically I don't really understand what this means.

So at this point I have the following questions:

1) Why do both of these exist?

2) Is one generally recommended over the other?

3) Why does PyCharm show a warning for one but not the other?

4) What does it mean that tf.__version__ is generated dynamically? How is tf.VERSION generated if a different way?

5) To avoid having to add the # noinspection PyUnresolvedReferences comment I'd prefer to use tf.VERSION, is there any reason not to do this?

6) Most of the examples within the TensorFlow repository https://github.com/tensorflow/tensorflow and the related repositories (ex. models https://github.com/tensorflow/models) use the tf.__version__ way but some use the tf.VERSION way, is there a reason for this?

Upvotes: 2

Views: 18778

Answers (2)

bers
bers

Reputation: 5773

In TensorFlow 2.0 RC, VERSION seems to be gone:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> print(tf.__version__)
2.0.0-rc0
>>> print(tf.VERSION)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'VERSION'
>>>

Upvotes: 0

P-Gn
P-Gn

Reputation: 24581

Shortest possible version of the answer

They are the same.

"Slightly" longer version of the answer

Both symbols are aliases of tensorflow.python.framework.versions.VERSION and are imported in the file tools\api\generator\api\__init__.py as

from tensorflow.python.framework.versions import VERSION
from tensorflow.python.framework.versions import VERSION as __version__

Going down the rabbit hole, the aliases eventually point to the __version__ of _pywrap_tensorflow_internal, which is basically tensorflow's C++ library which is indeed loaded dynamically -- in the end, python's __version__ is just an alias of TF_VERSION_STRING of the C++ API.

Now why would PyCharm emit a warning for the second, it could be a limitation (a bug?) of PyCharm's parsing that cannot cope with complex definitions of __all__ as the one defined at the end of the same file. Search for "PyCharm __all__" on this site and you will find some hints of that.

So in the particular case of tensorflow, both options should be fine, and you could use tf.VERSION if it removes a warning for you. However the __version__ idiom is more common (some would say standard) because it is a recommandation of PEP8, so I would possibly stick to tf.__version__.

Upvotes: 5

Related Questions