PyCharm 中的 TensorFlow Python 警告 - 在 __init__.py 中找不到参考 __version__ [英] TensorFlow Python warning in PyCharm - Cannot find reference __version__ in __init__.py

查看:94
本文介绍了PyCharm 中的 TensorFlow Python 警告 - 在 __init__.py 中找不到参考 __version__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

if tf.__version__ < x.x.x:

PyCharm 中的语句,可以在许多 TensorFlow GitHub 示例中找到,如下所示:

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()

这很好用,我目前在 TensorFlow 1.4.0,如果我像上面一样运行这个脚本,错误信息不会显示,如果我将 if 语句更改为,例如, 2.4.0(当然还没有出来)然后错误显示如预期,程序退出.

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.

我遇到的问题是 PyCharm 在 if 语句中显示以下警告:

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

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

截图如下:

如果我选择 PyCharm 灯泡图标,我会得到以下选项:

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

没有一个特别吸引人.

目前,我选择最后一个选项Suppress for statement",将这一行添加到 if 语句上方

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

# noinspection PyUnresolvedReferences

我担心的是,我正在编写许多其他人会使用的文档,其中一些人将使用 PyCharm,而其中一些人将不可避免地使用不同的编辑器.

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.

出于这个原因,我无法更改任何 TensorFlow init.py 文件,因为这会在我的计算机上创建自定义安装,并且任何遵循我的文档的人都会在他们的计算机上看到不同的结果如果他们正在使用 PyCharm,则进行筛选.此外,我使用 pip 来安装软件包,所以我什至不确定这是否可行,但即使是这样,它仍然不是一个可接受的选择.

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.

同样,我不希望包含特定于 PyCharm 的注释行,因为这会给不使用 PyCharm 的人造成混淆.

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

我真的不想完全禁用此检查,因为我发现 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.

建议的答案是编辑一个 init.py 文件,由于上述原因我不想这样做,或者更改导入语句以达到

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

这似乎不适用于我的情况,因为我完全使用语句导入 TensorFlow

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

import tensorflow as tf

即使修改 import 语句以防止出现此警告是可能的,我仍然宁愿不这样做,因为这会导致非 PyCharm 用户的混淆,并使我的示例与所有其他可用的 TensorFlow 文档和示例不同.

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?

推荐答案

tf.__version__ 是动态生成的,因此各种 import hacks 不太可能起作用.

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

解决警告的简单方法是使用 getattr喜欢:

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

import tensorflow as tf

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

这篇关于PyCharm 中的 TensorFlow Python 警告 - 在 __init__.py 中找不到参考 __version__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆