6

As I'm getting further into TensorFlow and the finer points of Python, I've noticed these 3 statements at the top of many (perhaps most) .py files in the TensorFlow repository:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

Here are two examples:

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

I should clarify that I have no interest or need to support Python 2 or substantially older versions of Python 3 in any projects I'm undertaking currently.

After reading about from __future__ import xyz statements, for example from these sources:

What is __future__ in Python used for and how/when to use it, and how it works

https://docs.python.org/2/library/future.html

https://docs.python.org/3/library/future.html

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

I'm able to understand most of this documentation but I'm left with the following questions:

  1. If only using Python 3, is there any need for these statements or can they safely be removed entirely in all cases? More specifically, if I put the following statement in the beginning of a program:

    # 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
    

    Can the above statements be removed with no possibility of ill effects?

  2. Would these statements matter in an older version of Python 3 vs a newer version of Python 3, ex. Python 3.1 vs Python 3.6?

  3. Since TensorFlow requires Python version 3.5 or later, if these statements only matter if Python 2 (or possibly an older version of Python 3? see the previous question) is being used, why are these included in the TensorFlow codebase?

  4. If removing these could ever cause a problem even when using a recent version of Python (ex. 3.5 or later), what would be an example to demonstrate such a problem?

-- EDIT --

user2357112 just pointed out that on Ubuntu TensorFlow supports Python 2.7 or Python 3.4:

enter image description here

I was honestly not aware of this as I'd been using the Windows version of TensorFlow, which requires at least Python 3.5:

enter image description here

So, I guess my question is specific to either a Windows TensorFlow install, or a TensorFlow install on a different OS using a recent version of 3.x.

cdahms
  • 3,402
  • 10
  • 49
  • 75
  • I don't know why you say TensorFlow needs Python 3.5+. The [site](https://www.tensorflow.org/install/install_linux) says 2.7 or 3.4+. – user2357112 May 19 '18 at 17:38
  • I didn't realize that on Ubuntu, TensorFlow could run on Python 2.7. On Windows, which I've been using so far, at least Python 3.5 is required to run TensorFlow. Thanks for pointing this out, I updated the question description applicably. – cdahms May 19 '18 at 17:55
  • Huh. I didn't know it required 3.5+ on Windows. We both learned something! – user2357112 May 19 '18 at 18:00
  • Note that python 2.7 end of life is scheduled for 01/01/2010, only 1 year and 7 months from now (yay). Most of those `from __future__ import` won't be a thing soon (hopefully). – P-Gn May 19 '18 at 18:54
  • I'm guessing you mean 1/1/2020. In any case you'd be surprised how many people try to hold onto outdated software and development environments to avoid having to make a few changes. There are still companies out there using Visual Basic 6. – cdahms May 19 '18 at 19:10

0 Answers0