4
votes

I'm using the

if tf.__version__ < x.x.x:

statement within PyCharm, which is found in many of the TensorFlow GitHub examples, like so:

# tensorflow_version_test.py

import tensorflow as tf

#######################################################################################################################
def main():
    if tf.__version__ < "1.4.0":
        print("ERROR: TensorFlow version is older than 1.4.0, upgrade to 1.4.0 or higher before continuing !!!")
        return
    # end if

    print("TensorFlow version is >= 1.4.0, continuing . . .")

    # rest of program would go here . . .

# end main

#######################################################################################################################
if __name__ == "__main__":
    main()

This works great, I'm at TensorFlow 1.4.0 currently, if I run this script as above, the error message does not show, and if I change the if statement to, for example, 2.4.0 (which is not out yet of course) then the error shows as expected and the program exits.

The problem I'm encountering is PyCharm shows the following warning on the if statement:

Cannot find reference '__version__' in '__init__.py'

Here is a screenshot:

enter image description here

If I choose the PyCharm light bulb icon, I get these options:

enter image description here

none of which are especially appealing.

For the moment, I'm choosing the last option "Suppress for statement", which adds this line above the if statement

# noinspection PyUnresolvedReferences

My concern here is I'm in the process of writing documentation that many other people will use, some of whom will be using PyCharm and some of whom will inevitably be using a different editor.

For this reason, I can't alter any of the TensorFlow init.py files, because that would then create a custom install on my computer and anybody following my documentation would see different results on their screen if they are using PyCharm. Further, I'm using pip to install packages so I'm not even sure if this is possible, but even if it is it's still not an acceptable choice.

Similarly, I'd prefer not to include a comment line specific to PyCharm since that would cause confusion for those not using PyCharm.

I'd really prefer to not disable this inspection entirely since I find PyCharm's warnings very helpful in many circumstances and therefore I'd rather not disable them.

Upon Googling on this concern, I found this post.

The suggested answers were to either edit an init.py file which I'd rather not do for the reasons mentioned above, or to change the import statement to be to the effect of

from somePackage import module1, module2, module3

which does not seem to be applicable in my case since I'm importing TensorFlow entirely with the statement

import tensorflow as tf

Even if modifying the import statement to prevent this warning is somehow possible I'd still rather not do this as this would cause confusion for non-PyCharm users and would make my examples different than all the other TensorFlow documentation and examples available.

At the moment it seems this is the best I can do:

    # this next comment line is necessary to avoid a false warning if using the editor PyCharm
    # noinspection PyUnresolvedReferences
    if tf.__version__ < "1.4.0":
        print("ERROR: TensorFlow version is older than 1.4.0, upgrade to 1.4.0 or higher before continuing !!!")
        return
    # end if

Is there something I'm missing here? Any suggestions as to a better way to do this?

1

1 Answers

2
votes

tf.__version__ is generated dynamically, so the various import hacks are unlikley to work.

A simple hack to work around the warning is using getattr like:

import tensorflow as tf

if getattr(tf, '__version__') < "1.4.0":
    ....