16.7 Reading and importing files into Sage

A file imported into Sage must end in .py, e.g., foo.py and contain legal Python syntax. For a simple example see §4.1 with the rubik's cube group example above.

Another way to read a file in is to use the load or attach command. Create a file called example.sage (located in the home directory of Sage) with the following content:

print "Hello World"
print 2^3
Read in and execute example.sage file using the load command.

sage: load "example.sage"
Hello World
8

You can also attach a Sage file to a running session:

sage: attach "example.sage"
Hello World
8
Now if you change example.sage and enter one blank line into Sage, then the contents of example.sage will be automatically reloaded into Sage:

sage: !emacs example.sage&     #change 2^3 to 2^4
sage:                          #hit return
***************************************************
                Reloading 'example.sage'
***************************************************
Hello World
16

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