ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 07 Mar 2013 20:24:57 +0100sage version of python's execfilehttps://ask.sagemath.org/question/9894/sage-version-of-pythons-execfile/What is the sage analogue of python's "execfile()"? How do I ask sage to preprocess and execute a text file full of sage commands?
Up till now I've just been using execfile() on the sage command line prompt, because that's what I do in python sometimes. However I just learned the hard way that this is the wrong thing to do; it misses a vital preprocessing step. For example, if I ask for the type of 1/2 on the sage command line, the following happens:
sage: print type(1/2)
<type 'sage.rings.rational.Rational'>
Great! that's what I want. However if I put the following into the file "uhoh.py":
print type(1/2)
and then I issue the following command on the sage command line:
execfile("uhoh.py")
then I get the output
<type 'int'>
Yes, the integer 1/2. I want the last hour of debugging time back.
Of course what's happening is that the text file is being interpreted in pure python, in which 1/2 really does represent an integer division. What is the right thing to do here, rather than execfile()?Benjamin YoungThu, 07 Mar 2013 20:24:57 +0100https://ask.sagemath.org/question/9894/