16

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.

user2699
  • 2,181
  • 16
  • 32
  • 3
    See http://emacs.stackexchange.com/questions/13244/edebug-orgmode-source-code-blocks-with-input-variables . 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 07: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
  • @user2699 - Are you familiar with `pdb`? I'm still learning python but `pbd` seems to have more advanced features. It looks like it can be turned on by adding the `:prologue "import pdb\npdb.set_trace()\n"` header. When the code block is executed you should be able to use interactive `pdb` commands in the `*my-python*` buffer. – Melioratus Dec 16 '15 at 02:59

1 Answers1

13

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

Melioratus
  • 4,504
  • 1
  • 25
  • 43