Ask Your Question

Import custom sage libraries into a Jupyter notebook

asked 2017-02-10 21:16:55 +0200

as_butler gravatar image

What is the correct way to write and import custom SAGE libraries into a jupyter notebook?

When using jupyter notebooks with a python kernel, importing your own python library is as easy as saving a file in the same directory as the notebook and putting animport foo line in the notebook. Using a SAGE kernel, I can import into my notebook, but without the ability to call SAGE methods: e.g. calling the function

def monty(n): return SymmetricGroup(n)

from gives the error,

NameError: global name 'SymmetricGroup' is not defined

My desired workflow: work in a notebook for convenience but be able to pass on what I've done in the form of a library.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-02-12 14:43:31 +0200

mforets gravatar image

updated 2017-02-12 14:51:30 +0200

To import:

  • code from other files (.py, .sage, ..) into a notebook, this can be done simply with the load command, as in load('').

  • via standard python imports: say your library is in foo.sage, then



sage --preparse 'foo.sage' # will import sage library (sage.all_cmdline) = everything so that Sage functions can be called externally, into a new file

mv # let's rename it


from foo import monty

edit flag offensive delete link more

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: 2017-02-10 21:16:55 +0200

Seen: 1,929 times

Last updated: Feb 12 '17