Ask Your Question

preparser(False) doesn't work with attach()

asked 2014-11-05 08:57:54 +0200

Polonius gravatar image

updated 2014-11-05 14:23:21 +0200

kcrisman gravatar image

This is a more basic version of my previous question, which had distracting details.


In William Stein's Sage for Power Users (, end of Section 2.2 and top of page 13, there is an example of how to turn the preparser off in the sage interpreter. The example works as claimed and here it is copied from my own computer:

sage: preparser(False)

sage: N=2/3 + 2^3

sage: N


sage: preparser(True)

sage: M=2/3 + 2^3

sage: M


But now suppose that I want to put (almost) exactly the same lines into a .sage file called 'preparsertest.sage', close sage, reopen it, and then type attach('preparsertest.sage') into the interpreter. Naively I expect the same behaviour but that's not what I get.

Here is the content of 'preparsertest.sage':


N=2/3 + 2^3



Here is the output from the interpreter:

sage: attach('preparsertest.sage')

sage: N


sage: M


So it seems that when I attach 'preparsertest.sage', the line preparser(False) is not being read.

What gives?

P.S. I tried to figure out how to attach a screen shot but it didn't work, and I didn't find instructions on

edit retag flag offensive close merge delete


I think you may have to have some minimum "karma" in order to attach screenshots. There is a little icon at the top of the question area next to the bold, italic, etc. which looks like a computer screen; probably it's disabled until a certain point. Sadly, this is a necessary thing given the amount of spam we get...

kcrisman gravatar imagekcrisman ( 2014-11-05 14:24:42 +0200 )edit

Thanks. How did you put the grey boxes around the 'code'?

Polonius gravatar imagePolonius ( 2014-11-06 10:52:46 +0200 )edit

When you are typing, you should see some icons (just like the ones I mentioned) - and another one of those looks like 101010 or something. Highlight the code, and then click that. It will indent each line 4 spaces (you can also do this by hand, of course) and that is what is required to tell askbot this is code.

kcrisman gravatar imagekcrisman ( 2014-11-07 15:13:42 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2014-11-06 19:15:25 +0200

William Stein gravatar image

"I thought that the following are equivalent: entering a bunch of lines into the sage interpreter, hitting return after each one, or putting those same lines into a .sage file and then attaching that .sage file in the interpreter." They are not equivalent. When you load a .sage file, Sage reads the file into memory, runs the preparser on it, then calls the execfile command on the resulting string. It thus runs the entire contents of the file in one single call to execfile.

More precisely, here's the actual source code in the sage library, from src/sage/misc/ (

elif fpath.endswith('.sage'):
    from sage.misc.attached_files import load_attach_mode
    load_debug_mode, attach_debug_mode = load_attach_mode()
    if (attach and attach_debug_mode) or ((not attach) and load_debug_mode):
        # Preparse to a file to enable tracebacks with
        # code snippets. Use preparse_file_named to make
        # the file name appear in the traceback as well.
        # See Trac 11812.
        execfile(preparse_file_named(fpath), globals)
        # Preparse in memory only for speed.
        exec preparse_file(open(fpath).read()) + "\n" in globals
edit flag offensive delete link more


OK. Thank you for the clarification. It seems the ultimate answer to my question is 'don't write .sage files if you want to use some pure python along the way'.

Polonius gravatar imagePolonius ( 2014-11-06 19:51:01 +0200 )edit

answered 2014-11-05 16:16:33 +0200

tmonteil gravatar image

updated 2014-11-05 16:19:19 +0200

This is perhaps not a satisfactory answer: when using attach, the fact that the file is preparsed or not depends on its name. If it ends with .sage then it will be preparsed, if it ends with .py then it will not be preparsed. It seems the preparsing is done before executing the files, so the preparser commands you write inside the files are useless (the preparsing was already done when those lines are executed).

So, if you want to use both preparsed and unpreparsed things within files, you should use a mix of files, some files ending with .py and some files ending with .sage.

Note that the preparsing is mainly a trick to make the interactive mode easier. If you aim at maintaining a set of files on the long term, it is advised to use only .py files, which are true Python. Note also that Sage source code is made of .py files and do not use any kind of preparsing, so this advice may be useful if you plan to let your code eventually enter Sage source code. If you want to migrate old .sage files into .py files, you can use the preparse function to help in the translation.

edit flag offensive delete link more


Thanks. Then there is something I really don't understand. I thought that the following are equivalent: entering a bunch of lines into the sage interpreter, hitting return after each one, or putting those same lines into a .sage file and then attaching that .sage file in the interpreter. Doesn't attach read the lines one by one? Anyhow, about .sage versus .py. I've been doing that. I write some .sage file that produces a bunch of matrices, makes them into numpy arrays, and the saves those numpy arrays into a file. Then I write another .py file which loads those numpy arrays and applies some python modules to them. But this is rather awkward and (at least the first time) time consuming. Maybe it is the situation not your answer that is unsatisfactory.

Polonius gravatar imagePolonius ( 2014-11-06 10:02:46 +0200 )edit

OK. Thanks. Now if I start writing .py files which mostly consist of sage, I suppose I'll have lots more questions for this website.

Polonius gravatar imagePolonius ( 2014-11-06 19:54:13 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-11-05 08:57:54 +0200

Seen: 630 times

Last updated: Nov 06 '14