Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

sage version of python's 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()?