Ask Your Question

Other than "Jupyter Notebook", is there any other way to use Sage?

asked 2018-07-10 09:25:30 +0200

yoyostein gravatar image

updated 2018-07-10 09:52:48 +0200

I am searching for a "compiler" way to use Sage.

Something like TeXShop, I write everything in a single file and press compile to get the results.

Is there an easy way to accomplish this?

Currently I am using the "Notebook" form of Sage which is neat but kind of gets in the way of more complicated programming.

(To be more clear, I am looking for something like an IDE "Integrated development environment", something like PyCharm for Python.)

Thanks a lot.

edit retag flag offensive close merge delete


You can find instructions for configuring PyCharm with sage here:

Iguananaut gravatar imageIguananaut ( 2020-02-04 15:24:15 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2018-07-10 10:42:54 +0200

slelievre gravatar image

Ways to use SageMath

On my computer

  • the sage REPL or command-line-interface
    • launch Sage in a terminal, wait for the sage: prompt
    • type a command then hit the ENTER or RETURN key, get output
  • the Jupyter Notebook with the Sage kernel
    • ways to launch it
      • in a terminal, type sage -n jupyter
      • or use a launcher such as
        • the macOS "Sage app"
        • the Windows launcher
        • a desktop launcher for Linux
  • JupyterLab
    • install using sage --pip install jupyterlab
    • then in the terminal run sage -n jupyterlab
  • the legacy SageNB notebook
    • ways to launch it
      • in a terminal, type sage -n sagenb
      • or in the Sage REPL, type notebook()
      • or use a launcher
  • run a command with sage -c
    • example: in a terminal, run sage -c "print(2 + 2)"
  • run a file with extension .py or .sage
    • put the commands in a file such as myfile.sage or
    • in the terminal, run sage myfile.sage or sage
    • difference between .sage and .py files
      • for .sage files, the Sage preparser will be used
      • for .py files, the Sage preparser will not be used
  • use external files
    • commands
      • load, runfile, attach, %load, %runfile, %attach
      • examples
        • suppose the file myfile.sage contains def sq(a): return a*a
        • in the terminal, run sage -c "load('myfile.sage'); print(sq(2))"
  • from another program
  • use OpenMath and SCSCP to exchange mathematical objects with other mathematics software; work in progress, see


  • SageCell
    • one-off computations on SageCell page
    • use it to include compute cells in a webpage
    • use it with PreTeXt
  • CoCalc
    • see dedicated item below
  • JupyterHub
    • there are some deployments of JupyterHub which offer the Sage kernel
  • mybinder
    • there are some mybinder instances that include Sage
  • Sage Notebook servers
    • there some deployments of the SageNB notebook; the one formerly at is no longer active


  • in a CoCalc terminal, run sage for the Sage REPL
  • use sage_select to select which version of Sage to use by default
  • CoCalc Sage worksheets (.sagews)
  • Jupyter Notebook worksheets (.ipynb)
    • using CoCalc's version of the Jupyter Notebook
    • using the Classic Jupyter Notebook
      • go to Project preferences and launch Classic Jupyter Notebook; this will open a separate browser tab which will connect to your project using the classic Jupyter Notebook protocol;
      • allows to use Jupyter Notebook extensions that are not yet implemented in "CoCalc Jupyter", such as widgets, RISE, ...

Sage: file formats, data formats

  • common file extensions
    • .py, .sage, .pyx, .spyx, ...
    • .sobj
    • SageNB notebook worksheets: .sws
    • CoCalc Sage worksheets: .sagews
    • .rst, .txt, .html
  • converters
    • rst2ipynb, ...
  • viewing, saving, copying, transferring worksheets

Run shell commands from within sage

  • any command starting with ! is executed as a shell command
  • many basic shell commands are available in IPython without !: ls, cd, pwd...
  • see also the Python modules os and sys

Read from, write to, append to file

  • this uses standard Python functionality
  • 'r' for read, 'w' for write (overwriting file), 'a' for append
  • read from file, all at once or line by line

    with open('/path/to/file.txt', 'r') as f:
        s =
    with open('/path/to/file.txt', 'r') as f:
        for line in f:
            <do something with line>
  • write to file, bit by bit or line by line

    with open('/path/to/file.txt', 'a') as f:
        f.writelines(['haha', 'hehe', 'hihi'])
  • see also the csv module for "comma-separated-values" data files

edit flag offensive delete link more



This is ridiculously comprehensive and should be on the main Sage website in a prominent place ...

kcrisman gravatar imagekcrisman ( 2018-07-12 19:11:57 +0200 )edit

answered 2020-01-15 16:58:43 +0200

56phil gravatar image

updated 2020-02-04 14:25:45 +0200

If one wants to use Sage in Jupyterlab on macOS Catalina 10.15.1 (19B88), this worked for me:

1. download Sage binaries
2. unpack download
3. move SAGE_ROOT to the desired location
4. set SAGE_ROOT and any other environment variables (e.g. SAGE_KEEP_BUILT_SPKGS) in ~/.zshrc
    (a) export SAGE_KEEP_BUILT_SPKGS="yes" 
    (b) export SAGE_NUM_THREADS=3
    (c) export SAGE_ROOT="$HOME/SageMath"
5. Update $PATH by editing /etc/paths Note: You’ll need to use SUDO.
6. cd $SAGE_ROOT && ./sage -i openssl && ./sage -f python3 && make ssl && sage –pip3 install jupyterlab

when all this is done, sage -n jupyterlab, should work.

Update: Avoid updating Python packages using pip. I broke the sage kernal in jupyterlab doing this.

edit flag offensive delete link more



This has been discussed a few times on sage-devel. Given the ongoing switch to Python 3, which shall enable the upgrade of a lot of Python libraries in Sage (namely ipython, jupyterund so weider...), the addition of an "official" Jupyterlab interface is probably a question of time...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-01-15 18:55:09 +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



Asked: 2018-07-10 09:25:30 +0200

Seen: 8,431 times

Last updated: Feb 04 '20