I would like to be able to report warnings and errors when evaluating a Python code block in an Org file.

When a code block has Python syntax errors, Org helpfully pops them up in a separate buffer called *Org-Babel Error Output*. It would be nice to be able to send my own warnings there too, while retaining the ability to return an array result to Org mode as a table using :results value.

To be clear, here is a dummy example of the kind of thing I thought might work:

#+BEGIN_SRC python 
    import sys
    return [['Table', 'header'], ['table', 'data']]

| Table | header |
| table | data   |

Edited to add: This discussion on the org-mode list suggests that error output is only displayed when the code-block evaluation fails. So it looks like this is possible, but only by aborting the evaluation using exit(1) or similar.

  • Constantine's answer works perfectly for me, if it is also a solution for you please accept the answer, if it isn't update your question, please. – atevm Feb 25 '18 at 21:51

As described by Eric Schulte on the org-mode mailing list (see the link in the question) we can modify org-babel-eval to achieve this:

(defvar org-babel-eval-verbose t
  "A non-nil value makes `org-babel-eval' display")

(defun org-babel-eval (cmd body)
  "Run CMD on BODY.
If CMD succeeds then return its results, otherwise display
STDERR with `org-babel-eval-error-notify'."
  (let ((err-buff (get-buffer-create " *Org-Babel Error*")) exit-code)
    (with-current-buffer err-buff (erase-buffer))
      (insert body)
      (setq exit-code
             (point-min) (point-max) cmd err-buff))
      (if (or (not (numberp exit-code)) (> exit-code 0)
              (and org-babel-eval-verbose (> (buffer-size err-buff) 0))) ; new condition
            (with-current-buffer err-buff
              (org-babel-eval-error-notify exit-code (buffer-string)))

Now if org-babel-eval-verbose is set to t, output printed to stderr during evaluation of a source code block show up in a separate window just as if source code evaluation failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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