Why does using from __future__ import print_function breaks Python2-style print?
First of all, from __future__ import print_function
needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print
as a function now. That's the whole point of from __future__ import print_function
; to bring the print
function from Python 3 into Python 2.6+.
from __future__ import print_function
import sys, os, time
for x in range(0,10):
print(x, sep=' ', end='') # No need for sep here, but okay :)
time.sleep(1)
__future__
statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning. From the documentation:
A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.
The documentation also mentions that the only things that can precede a __future__
statement are the module docstring, comments, blank lines, and other future statements.
Related videos on Youtube
UHMIS
Updated on September 09, 2020Comments
-
UHMIS over 3 years
I am new at programming with python, and I am trying to print out with a separator and end but it is still giving me a syntax error.
I am using python 2.7.
Here is my code:
from __future__ import print_function import sys, os, time for x in range(0,10): print x, sep=' ', end='' time.sleep(1)
And here is the error:
$ python2 xy.py File "xy.py", line 5 print x, sep=' ', end='' ^ SyntaxError: invalid syntax $
-
jonrsharpe over 8 yearsYou imported print as a function but you're still treating it as a statement.
-
Charlie Parker over 7 yearsyou can't call print without the parenthesis because you have changed print to be a function print(args)
-
Helen almost 6 years
-
CrazyVideoGamer over 3 yearsYou are still using python 2 print syntax, but you imported the print function from the future (aka the print function inside python 3). It replaces the old print syntax with the new syntax, thus creating an error
-
CrazyVideoGamer over 3 yearsAlso, I don't know why you are still using python 2, when python 3 is out, but whatever.
-
-
UHMIS over 8 yearsThanks... However, now it is printing 0123456789 instead of 0 1 2 3 4 5 6 7 8 9. how do I solve that?
-
Cyphase over 8 years@UHMIS, do
end=' '
. -
Cyphase over 8 years@UHMIS, it's not useless, it's just not useful in that particular code, because you're only printing one thing per call to
print
. If you were doing, for example,print(x, x**2, sep=' ', end=' ')
, it would be useful there because it's putting a separator between each item (in this case,x
andx**2
). Of course, the defaultsep
is' '
, so you don't have to specify that anyway. -
Cyphase over 8 years@AvinashRaj, can you show me what you mean?
-
Cyphase over 8 years@AvinashRaj, let's talk about your answer in the comments to your answer, to make it easier for posterity :).
-
Avinash Raj over 8 years@Cyphase then why he says that my code is working?
-
Cyphase over 8 years@AvinashRaj, I don't know; you'd have to ask UHMIS. But as I said in a comment to your answer, perhaps OP made a change and didn't mention it. And OP's first comment was that there was still an error.
-
Cyphase over 5 years@not2qubit I reverted the edit you just made, and I wanted to let you know why, since I know it was done in good faith. While the table was obviously related to
__future__
, it didn't really help answer the question or directly expand on the answer; it was just some extraneous related info. Also I'm not sure what exactly your intention was behind italicizing "docstring, comments, blank lines". Please feel free to respond if you want to discuss it, or if you'd like to comment with that link to the__future__
docs. Thanks for wanting to improve my answer. -
not2qubit over 5 years@Cyphase No problem. I just wanted to show what other features was available, along with when they were introduced, since that would be a natural followup question and would have helped clarify what they are used for.