3.9 Saving and Loading Complete Sessions

Sage has very flexible support for saving and loading complete sessions.

The command save_session(sessionname) saves all the variables you've defined in the current session as a dictionary in the given sessionname. (In the rare case when a variable does not support saving, it is simply not saved to the dictionary.) The resulting file is an .sobj file and can be loaded just like any other object that was saved. When you load the objects saved in a session, you get a dictionary whose keys are the variables names and whose values are the objects.

You can use the load_session(sessionname) command to load the variables defined in sessionname into the current session. Note that this does not wipe out variables you've already defined in your current session; instead, the two sessions are merged.

First we start Sage and define some variables.

sage: E = EllipticCurve('11a')
sage: M = ModularSymbols(37)
sage: a = 389
sage: t = M.T(2003).matrix(); t.charpoly().factor()
 _4 = (x - 2004) * (x - 12)^2 * (x + 54)^2

Next we save our session, which saves each of the above variables into a file. Then we view the file, which is about 3K in size.

sage: save_session('misc')
Saving a
Saving M
Saving t
Saving E
sage: quit
was@form:~/tmp$ ls -l misc.sobj
-rw-r--r--  1 was was 2979 2006-01-28 19:47 misc.sobj

Finally we restart Sage, define an extra variable, and load our saved session.

sage: b = 19
sage: load_session('misc')
Loading a
Loading M
Loading E
Loading t
Each saved variable is again available. Moreover, the variable $ b$ was not overwritten.

sage: M
Full Modular Symbols space for Gamma_0(37) of weight 2 with sign 0 
and dimension 5 over Rational Field
sage: E
Elliptic Curve defined by y^2 + y = x^3 - x^2 - 10*x - 20 over Rational 
Field
sage: b
19
sage: a
389

See About this document... for information on suggesting changes.