"from __future__ import print_function" fails on Sage scripts
Hello, Sage Community.
I am trying to use the "print" function in a .sage script, so I have added the line
from __future__ import print_function
as the first line to be included in my .sage file. Unfortunately, when running sage test.sage
, Sage preparses the document and creates an auxiliary file "test.sage.py", which makes an import, then predefines some constants, and finally adds my preparsed code. As a consequence, the from __future__ import print_function
is not the first line, and I get the following error message:
File "test.sage.py", line 6
from __future__ import print_function
SyntaxError: from __future__ imports must occur at the beginning of the file
Here is a MWE. The file "test.sage" containing:
from __future__ import print_function
print(1+1)
is preparsed to "test.sage.py" containing:
# This file was *autogenerated* from the file test.sage
from sage.all_cmdline import * # import sage library
_sage_const_1 = Integer(1)
from __future__ import print_function
print(_sage_const_1 +_sage_const_1 )
Of course, I could manually add this line to the .sage.py file and then execute it, but this could be tedious in my case for two reasons: 1. I have a lot of files which I have to modify and rerun every 15 minutes. 2. I also need this process to be automatic to be able to execute it with automatically generated script and even sageTeX.
Thanks in advance for your answers!
Just use .py files. Preparsing is not really useful once you are used to sage.
The problem is that Sage executes .py files as purely Python files, so commands like
plot
are not recognized. Of course I can use Sage as a library an import it into the .py file, but unfortunately, the software I am working with require to first build .sage files.Then just import what you need. The command "import_statements" is very useful to find the required imports.
This seems like something that could be fixed in the .sage file preparser.
Do you need to include the line
from __future__ import print_function
?print(x)
should work fine without it.Yes,
print(x)
will work in Python2 and Python3. However, things likeprint()
andprint('text', end='')
only work in Python3. Actually,print()
prints()
in Python2, and it should display an empty line.Hello, @John Palmieri. Yes, I could fix my problem following your comment: I modified the
sage-prepase
files to add that import line, and that did the magic! Since it was your idea, would you like to post it as answer?, or would you prefer me to do it directly?This is a deficiency in the preparser: this is an example of possible valid python input into the preparser that gets preparsed into iinvalid python instructions. The preparser shouldn't do that. We should check if there is a
from __future__ ...
as a first (non-commented?) line in the file and put that on top. It's nice to have a work-around, but it's really a fixable bug. Anybody file a ticket? For the official rules about what a valid location for a "__future__" line is: https://docs.python.org/2/reference/s...I'd suggest just taking all the "from __future__ ..." lines from the content and placing them on top. That way we could end up transforming invalid code into valid code, but that's not such a crime.