I'm trying to use sage-math within an org-mode file. So far I've been able to configure the sage-shell-mode package (I'm impressed that sagemath can be installed in Debian using the package manager), my configuration:

(setq sage-shell:sage-executable "/usr/bin/sage")

;; Turn on eldoc-mode
(add-hook 'sage-shell-mode-hook #'eldoc-mode)
(add-hook 'sage-shell:sage-mode-hook #'eldoc-mode)

(setq sage-shell:use-prompt-toolkit t)
(setq sage-shell:completion-function 'pcomplete)

Then, I installed ob-sagemath from the MELPA repository and followed the instructions from its github page. Notice that I've added a line requiring the package, that it's not in the instructions... but don't work either!

(require 'ob-sagemath)
;; Ob-sagemath supports only evaluating with a session.
(setq org-babel-default-header-args:sage '((:session . t)
                                           (:results . "output")))

;; C-c s for asynchronous evaluating (only for SageMath code blocks).
(with-eval-after-load "org"
  (define-key org-mode-map (kbd "C-c s") 'ob-sagemath-execute-async))

;; Do not confirm before evaluation
(setq org-confirm-babel-evaluate nil)

;; Do not evaluate code blocks when exporting.
(setq org-export-babel-evaluate nil)

;; Show images when opening a file.
(setq org-startup-with-inline-images t)

;; Show images after evaluating code blocks.
(add-hook 'org-babel-after-execute-hook 'org-display-inline-images)

(eval-after-load "sage-shell-mode"
  '(sage-shell:define-keys sage-shell-mode-map
     "C-c C-i"  'helm-sage-complete
     "C-c C-h"  'helm-sage-describe-object-at-point
     "M-r"      'helm-sage-command-history
     "C-c o"    'helm-sage-output-history))

(setq sage-shell:input-history-cache-file "~/.emacs.d/.sage_shell_input_history")

(add-hook 'sage-shell-after-prompt-hook #'sage-shell-view-mode)

Now, I open an org-mode file and try an example:

#+begin_src sage :exports both

and I get a comment in the *Messages* buffer

executing Sage code block... ob-sagemath--last-res-info: Invalid output: --------------------------------------------------------------------------- NameError Traceback (most recent call last) in () ----> 1 _emacs_ob_sagemath.read_file_and_run_cell("/tmp/sage_shell_mode7254uF0/sage_shell_mode_temp.sage", filename=None, latex=False, latex_formatter=None)

NameError: name ’_emacs_ob_sagemath’ is not defined

Complementary info:

From my .emacs

(org-babel-do-load-languages 'org-babel-load-languages 
  '((C . t)
    (emacs-lisp . t) 
    (fortran . t)
    (gnuplot . t)
    (ipython . t)
    (latex . t)
    (ledger . t)
    (python . t)
    (mathematica . t)
    (maxima . t)
    (octave . t)
    (org . t)
    (R . t) 
    (shell . t)

(setq org-confirm-babel-evaluate nil)


  • What is happening?
  • How do I solve it?

1 Answer 1


Probably it is not a problem for you anymore after 2 years, but I had exactly the same problem. I got it working now, and I think the reason was that in my .emacs I was configuring sage-shell-mode (in addition to ob-sagemath), but I didn't have it installed. Installing sage-shell-mode fixed it for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.