clstaudt
clstaudt

Reputation: 22438

Make IPython show source code line where exception occurred

I'm irritated by the following output that IPython gives me for an exception:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-4c27956d23f6> in <module>()
----> 1 parameterStudyPageRankNibble()

/Users/cls/Desktop/LFR/scripts/scd_experiments.py in parameterStudyPageRankNibble()

AttributeError: 'tuple' object has no attribute 'values'

What can I do to get more helpful information when an exception is thrown?

Upvotes: 2

Views: 366

Answers (2)

Andreas R&#246;hler
Andreas R&#246;hler

Reputation: 4804

WRT answer by unutbu:

python-mode.el should not say just "module", but display the name of the buffer or file called, something seems broken:

https://bugs.launchpad.net/python-mode/+bug/1318991

Also must read imports by default when executing source, no explicit setting of PYTHONPATH required here.

It might be worth mentioning in context:

py-execute-... commands may use backends different from default according to options

  • python-mode-v5-behavior-p
  • py-execute-no-temp-p
  • py-fast-process-p

python-mode.el v5 series used shell-command-on-region. At that time existed bug lp:550661, WRT unicode symbol u'\xA9', ©

That bug was avoided by running the stuff from a temporary file. Which is the default still. An error-message sent from Python than points to the temporary file - which requires to be dealt with at the Emacs side.

As a user wanted to stay with simpler old proceeding, python-mode-v5-behavior-p was provided as an option.

In order to speed up and/or execute on systems without write-permission, a newer backend avoids writing temporary files. Internally relies on process-send-string. Customize py-execute-no-temp-p to enable it.

With v6 series stuff is processed by an interactive (I)Python shell. That allows further interactive investigations. The backside was shown by lp:1253907 - with large output, the comint bookkeeping might make Emacs slow, resp. freeze.

Thus option `py-fast-process-p' was introduced - see menu "Python/Fast process..." Avoids Python shell, results arrive in buffer "*Python Output*".

With version 6.1.3, optional py-fast-process-p is not released yet, available in trunk.

Upvotes: 0

unutbu
unutbu

Reputation: 879501

This has to do with how you load the code into IPython. (It would be helpful if you told us how to reproduce your problem.) For example, if I use emacs's M-x py-execute-region, the region of code is copied to a temporary file and the IPython interpreter runs execfile on the temporary file.

When emacs/IPython is used in this manner, there is no useful traceback.

If instead, you import the script and then call the function:

import scd_experiments as SE
SE.parameterStudyPageRankNibble()

then you will see a useful traceback message.


Demo: script.py

def foo():
    1/0

If I select the region and call M-x py-execute-region,

In [15]: ## working on region in file /tmp/python-4126aNj.py...

In [16]: foo()
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-16-624891b0d01a> in <module>()
----> 1 foo()

/tmp/python-4126aNj.py in foo()

ZeroDivisionError: integer division or modulo by zero

But if I import the module:

In [19]: import script
In [20]: script.foo()
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-20-3ae0d96fd279> in <module>()
----> 1 script.foo()

/home/unutbu/pybin/script.py in foo()
      8 
      9 def foo():
---> 10     1/0

ZeroDivisionError: integer division or modulo by zero

Upvotes: 1

Related Questions