I have discovered that I can't use the flush
argument in the new print function if I use Python 2.7.11. I have used:
from __future__ import print_function
print('Hello', flush=True)
but it complains with the error:
Traceback (most recent call last):
File "print_future.py", line 3, in <module>
print('Hello', flush=True)
TypeError: 'flush' is an invalid keyword argument for this function
After looking at the documentation for print
I discovered that it doesn't work even though it is an argument. My inference is that it doesn't work because flush
was only added in version 3.3, thus the special from __future__ import
statement is probably importing an older version of the function. The comments suggest it's using 2.7.11 but I don't understand why it's doing this.
I did see Need Python 3.4 version of print() from __future__, whose answer just wraps print
and manually adds the flush
parameter. Even though that works, it seems more of a hack than addressing the real problem, that we don't have the most recent version of print.
Is there a way to import specific versions of Python functions (I want to use the Python 3.5 print
function, specifically) to my current Python script? If this is not possible, why not?
It seems (surprisingly) its not clear to people how my question is different even though I wrote it. I will say it differently.
- It seems that my issue is that its importing the python function that I am not expecting (since it doesn't find the flush argument). Therefore, the most natural and the first thing I'd like to do and know is, which print function is it importing. The comments suggest its using 2.7.11 but I don't understand why its doing this.
- I understand that the from future statement changes how my compiler works. It seems I assumed that since it was a future statement it also brought in the print function from a future release. It seems it only changes the behaviour of my interpreter. If I were able to see what print function its using I would know that its not importing a function from a future release but only acting like a future python interpreter. Thats what it seems but I don't know for sure and I'd like to know for sure whats going on.
- Last but not least, is there not a natural way to import future python functions to my current python script? I want to use python 3.5 print statement, is it not possible to use that function apart from just making the compiler behave like a future python interpreter but also behave like a future python version? It was (at least to me) counter intuitive to have the parser act like future python but still act like a 2.7.11. The solution I want is not a wrapper but a import of the recent python. If this is not possible, then a answer to my question should explain why its not possible.