[IPython-user] How does one get sys.last_traceback in ipython?

R. Bernstein rocky at panix.com
Sun Feb 26 08:01:15 CST 2006


In the stock python interpreter if one imports sys and runs a program
that raises an error, sys.last_traceback is set.

However in ipython sys.last_traceback is not set. (My guess is because
ipython fields the request.)

To make things more concrete here are some sample sessions.

% python
Python 2.4.2 (#1, Feb  8 2006, 13:24:02) 
[GCC 4.1.0 20060206 (Red Hat 4.1.0-0.23)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
import sys
>>> sys.last_traceback
sys.last_traceback
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'last_traceback'
>>> import gcd
import gcd
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "gcd.py", line 37, in ?
    check_args()
  File "gcd.py", line 16, in check_args
    raise Exception, "Need to give two numbers"
Exception: Need to give two numbers
>>> sys.last_traceback
sys.last_traceback
<traceback object at 0xb7f24414>
>>> 

% ipython
Python 2.4.2 (#1, Feb  8 2006, 13:24:02) 
Type "copyright", "credits" or "license" for more information.

IPython 0.7.1.fix1 -- An enhanced Interactive Python.
...

In [1]: import sys
import sys

In [2]: import gcd
import gcd
---------------------------------------------------------------------------
exceptions.Exception                                 Traceback (most recent call last)

/home/src/external-cvs/pydb/test/<ipython console> 

/home/src/external-cvs/pydb/test/gcd.py 
     35     return gcd(b-a, a)
     36 
---> 37 check_args()
     38 
     39 (a, b) = sys.argv[1:3]

/home/src/external-cvs/pydb/test/gcd.py in check_args()
     14     if len(sys.argv) != 3:
     15         # Rather than use sys.exit let's just raise an error
---> 16         raise Exception, "Need to give two numbers"
     17     for i in range(2):
     18         try: 

Exception: Need to give two numbers

In [3]: sys.last_traceback
sys.last_traceback
---------------------------------------------------------------------------
exceptions.AttributeError                            Traceback (most recent call last)

/home/src/external-cvs/pydb/test/<ipython console> 

AttributeError: 'module' object has no attribute 'last_traceback'

In [4]: 




More information about the IPython-user mailing list