4

Currently, when I

from __future__ import print_function

from Python 2.7.6, I apparently get a version of print() prior to the addition of the flush keyword argument, which went in Python 3.3 according to the docs. The Python3 installed in my system (Ubuntu) is Python 3.4, and I verified its print() function has the flush argument.

How do I import the print() function from 3.4? From where is __future__ getting the older print function?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Pete
  • 16,534
  • 9
  • 40
  • 54

1 Answers1

12

You cannot get the version from 3.4 imported into Python 2.7, no. Just flush sys.stdout manually after printing:

import sys

print(...)
sys.stdout.flush()

Or you can create a wrapper function around print() if you have to have something that accepts the keyword argument:

from __future__ import print_function
import sys
try:
    # Python 3
    import builtins
except ImportError:
    # Python 2
    import __builtin__ as builtins


def print(*args, **kwargs):
    sep, end = kwargs.pop('sep', ' '), kwargs.pop('end', '\n')
    file, flush = kwargs.pop('file', sys.stdout), kwargs.pop('flush', False)
    if kwargs:
        raise TypeError('print() got an unexpected keyword argument {!r}'.format(next(iter(kwargs))))
    builtins.print(*args, sep=sep, end=end, file=file)
    if flush:
        file.flush()

This creates a replacement version that'll work just the same as the version in 3.3 and up.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Great work-around; I guess `__future__` things are whatever happened to be packaged with that version of Python 2.x? – Pete Jan 16 '15 at 19:50
  • 2
    `__future__` imports change syntax parsing. This one disables recognition of `print` as a keyword. That allows normal access to the builtin `print()` function that is always present but normally masked by recognition of `print` as keyword. Without the `__future__` import, it can be accessed with `import __builtin__ as b; b.__dict__['print']`. – Terry Jan Reedy Jan 16 '15 at 20:15
  • 2
    @Pete: indeed, I was being imprecise. `from __future__` imports are syntax flags; the parser and compiler alter behaviour. The `print()` function is just a built-in in Python 2, but you cannot normally use it because `print` is a reserved keyword and a statement. With the import, the compiler removes that keyword reservation and statement. – Martijn Pieters Jan 16 '15 at 21:10
  • why can't you import the one from 3.4? – Charlie Parker Aug 28 '16 at 17:38
  • @CharlieParker because it is a built-in function defined in C code. – Martijn Pieters Aug 28 '16 at 17:54
  • @MartijnPieters how do I see what I am even importing? I also have a import future print statement and its not recognizing flush. – Charlie Parker Aug 28 '16 at 18:04
  • @MartijnPieters why does it matter if its a C function defined in C code? It seems that we are still importing it and using it regardless of that. – Charlie Parker Aug 28 '16 at 18:15
  • 2
    @CharlieParker: `__future__` imports don't actually import anything in the usual sense. Doing `from __future__ import print_function` is like telling Python 2.7 "I want to use `print` differently". It doesn't actually load any code from Python 3. It just tells Python 2.7 to use a different "mode" that is already built into Python 2.7. – BrenBarn Aug 28 '16 at 18:59