11

Are there any tools available to make tracing errors in org-mode blocks easier? (I'm working with python specifically)

My current workflow is to copy code that works to a source block, and try to make only minimal modifications. If major errors occur, I either copy it back to another file, or tangle the block and debug the resulting file.

Are there any tools I'm missing? (eg, something like compilation mode where it would take you to the line the error is on.)

Edit: for example, I just found C-c C-v v, which brings up a buffer with noweb references expanded. Unfortunately, it doesn't include header arguments, so the code from this can't be run without importing those in the interpreter first. Similarly, if I have a return statement, that keeps it from being executed, and the buffer isn't editable, so takes several steps to get runnable source from a python block.

  • 3
    See emacs.stackexchange.com/questions/13244/… . My current favorite solution is (org-src-debug). This has the advantage that you can debug source code blocks with input arguments. – Tobias Dec 12 '15 at 22:17
  • Comment for others with similar problems: user2699 edited the question to make clear that he wants to debug python source code blocks. The last comment of Tobias only applies to the execution of emacs-lisp code blocks. – Tobias Dec 15 '15 at 7:22
  • 1
    Did you add a :session header to your code block? e.g. :session *my-python*. Then just watch script execution in *my-python* buffer in another frame or window. I guessing you want something more sophisticated than this correct? – Melioratus Dec 15 '15 at 20:35
  • @Melioratus, it is a bit primitive (doesn't seem to handle errors nicely, and adds extra output in the python console), but it's much better than the nothing. And the python console is very nice. You should submit that as an answer. – user2699 Dec 15 '15 at 21:14
  • @Tobias, Thanks, that's the sort of thing I'm looking for, just not specific to elisp. – user2699 Dec 15 '15 at 21:17
7

Method 1

  1. Add :session header to your source block.

    In the example, using the :session header will execute the code inside a buffer named *my-python* and display debug messages.

    #+BEGIN_SRC python :session *my-python*
      'Hello {0}\n'.format('World')
    #+END_SRC
    

    Tips:

    • Use C-c C-v C-z to split screen vertically and display interactive *my-python* buffer.
    • Use C-c C-v ? to display other useful shortcuts.

Method 2

  1. Place point in code block and edit block using C-c ', i.e. org-edit-special. This method will allow you to use python-mode built-in functionality.

  2. Start python interpreter with C-c C-p, which will split the window and open the buffer name *Python*. To switch to python interpreter buffer press C-c C-z.

    Note: Recently I noticed that a *Warnings* buffer displayed instead. Just place point inside *Warnings* buffer and press q to display *Python* buffer.

  3. When finished, propagate your updated code into code block with C-c ' or discard any changes with C-c C-k.


Note this code was tested using:
emacs version: GNU Emacs 26.1
org-mode version: 9.1.14

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.