1

Or are there specific situations where one is better than the other?

So far, all I gathered is that future is only available for >=2.6 or >=3.3.

The current code I have is very basic and runs the same on python2 and 3 except for the use of print function calls. However, the code may get more complex over time and I would like to use the right approach for writing python2/3 compatible code from the beginning.

Lolo
  • 3,935
  • 5
  • 40
  • 50
  • I don't believe that this question fits in the scope of stack overflow. Answers might be quickly outdated as python 2 nears end of life. Currently the best guide I know for writing 2/3 compatible code is in the python-future library docs. http://python-future.org/compatible_idioms.html – Håken Lid Apr 03 '18 at 17:08
  • This question seems to be a bit confused. You definitely want to use `__future__` statements for writing cross-version code. The third-party `future` library requires those `__future__` statements in your code, it doesn’t replace the need for them. Meanwhile, almost all dual-version code requires at least 2.6+/3.2+, and most of it nowadays requires 2.7/3.4+, because that makes things a lot easier, and it’s very rare that you need code the works on 2.4 or 3.1. – abarnert Apr 03 '18 at 17:09
  • Other alternatives (or complements) to `future` include `six` and `modernize`. Which one is best depends on whether you’re primarily developing on 3.x or 2.x, whether you’re writing direct dual-version code or code to be installed with `2to3` or `3to2`, and which library’s style you prefer. They all have nice docs, so read them first. – abarnert Apr 03 '18 at 17:12
  • @abarnert Thanks for the clarification: I hadn't realized future used __future__ under the hood. Based on your and other comments, I will make python3 my primary target but I still need to support clients on python2. Requires >=2.7 seems acceptable based on what you say. – Lolo Apr 03 '18 at 17:15
  • Possible duplicate of [Writing cross-compatible Python 2/3: Difference between \_\_future\_\_, six, and future.utils?](https://stackoverflow.com/questions/42110826/writing-cross-compatible-python-2-3-difference-between-future-six-and-fut) – Jacques Gaudin Apr 03 '18 at 17:24

3 Answers3

4

The __future__ module is built-in to Python, and is provided to allow programmers to make advance use of feature sets which are not yet regarded as complete. Although some of the features (e.g., from __future__ import print_function) are provided specifically to assist with porting Python 2 programs to Python 3, it is also used to give early access to advance features of any release.

__future__ is unique, in that some imports such as the print_function can actually change the syntax accepted by the interpreter.

python-future is a third-party module, one of several to provide compatibility features. You could also take a look at six, though it's now somewhat long in the tooth, and python-modernize. It's quite likely you will find that you need to use both __future__ and future together.

Another strategy you may not have considered is to convert your source to Python 2 that can be translated automatically by the 2to3 utility. There is also a lib3to2 that will translate the other way, though I have no experience with it.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
2

Python 2.7 will not be maintained past 2020, see https://pythonclock.org/

Thus, if you just started learning python, I would suggest you just use python 3 directly instead of using python 2 and importing __future__.

gahooa
  • 131,293
  • 12
  • 98
  • 101
idchiang
  • 71
  • 5
1

Unless you have a very specific reason to support Python 2.x, then Python 3.x is the future. It's been over 10 years since it was released.

Python 2.7, the last of the 2.x series, is end of life'd in 2020. https://www.python.org/dev/peps/pep-0373/

TLDR: It's time to use Python 3

gahooa
  • 131,293
  • 12
  • 98
  • 101
  • 1
    I agree, but if you can also provide 2.7 compatibility those who are unable to migrate will be grateful. – holdenweb Apr 03 '18 at 17:22