Skip to content

Attaching Sage files to sage worksheets

William Stein edited this page Jun 12, 2018 · 4 revisions

QUESTION: Is there a way to write functions in one worksheet, and then important them to another and use them there?

Not exactly, but you can write code in a .sage file, then load it into another worksheet.

(1) Put code in a new file with extension ".sage", e.g., code.sage

(2) In a Sage worksheet or the terminal, run this: "%attach code.sage" or attach("code.sage")

(3) Now all code in code.sage is available in your worksheet, and whenever it changes, it will get reloaded automatically.

(4) If you're using Jupyter, this is all broken (see https://cocalc.com/share/4a5f0542-5873-4eed-a85c-a18c706e8bcd/support/2018-06-12-sage-code/?viewer=share), but at least you can use load("code.sage") instead.

Clone this wiki locally