Python 3-“ from __future__ import xyz”语句是否必要/有益(强调TensorFlow)?

cda*_*hms 6 python python-3.x tensorflow

随着我进一步深入TensorFlow和Python的优点,我注意到这3条语句在TensorFlow存储库中的许多(也许是大多数).py文件的顶部:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)

这是两个示例:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_deep.py#L25

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/word2vec/word2vec_basic.py#L17

我需要澄清的是,我目前在从事的任何项目中都没有兴趣或不需要支持Python 2或实质上较旧的Python 3版本。

阅读完有关from __future__ import xyz语句的信息后,例如从以下来源:

Python中的__future__是什么,以及如何/何时使用它以及如何工作

https://docs.python.org/2/library/ 将来的 .html

https://docs.python.org/3/library/ 未来 .html

http://python-future.org/imports.html

我能够理解本文档的大部分内容,但还有以下问题:

  1. 如果仅使用Python 3,是否需要这些语句,或者是否可以在所有情况下安全地将其完全删除?更具体地说,如果我将以下语句放在程序的开头:

    # check Python version is at least 3.6, if not, show an error and bail
    if sys.version_info.major < 3 or sys.version_info.minor < 6:
        print("ERROR: currently running Python version " + sys.version + ", at least version 3.6 is required")
        return
    # end if
    
    Run Code Online (Sandbox Code Playgroud)

    可以删除上面的陈述而不会造成不良影响吗?

  2. 这些陈述在旧版本的Python 3与新版本的Python 3中比较重要吗?Python 3.1和Python 3.6?

  3. 由于TensorFlow需要Python 3.5或更高版本,因此如果这些语句仅在使用Python 2(或者可能是Python 3的较旧版本?请参见前面的问题)时才重要,那么为什么这些包含在TensorFlow代码库中?

  4. 如果即使在使用最新版本的Python(例如3.5或更高版本)时删除它们仍可能导致问题,那么如何举例说明这种问题呢?

-编辑-

user2357112刚刚指出,在Ubuntu上TensorFlow支持Python 2.7或Python 3.4:

在此处输入图片说明

老实说,我一直没有意识到这一点,因为我一直在使用Windows版本的TensorFlow,它至少需要Python 3.5:

在此处输入图片说明

因此,我想我的问题特定于Windows TensorFlow安装或使用最新版本的3.x在不同操作系统上的TensorFlow安装。