Search code examples
pythonipythontraceback

Why isn't IPython giving me a full traceback for a module I've written?


I'm confused about why, when an error is raised in a function within a module I've written, IPython doesn't show me a full traceback with the line in the function that caused the error.

Note: I'm not confused about the cause of this particular error, but about why IPython isn't showing me the cause.

My module is called module.py and it contains the function function, underneath which is written an if __name__ == '__main__' block. (Module and function names have been changed to protect the identities of the innocent -- or maybe not so innocent.)

Here's the traceback I get when an error is raised. (Note the lack of information about which line in function caused the error.)

In [1]: import module as m

In [2]: call = m.function('hello')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-ec0c1e40ec8c> in <module>()
----> 1 call = m.function('hello')

/home/module.py in function(greeting)

TypeError: join() takes exactly one argument (2 given)

Solution

  • Did you try with %xmode ?

    In [2]: %xmode?
    Type:       Magic function
    Definition: %xmode(self, parameter_s='')
    Docstring:
    Switch modes for the exception handlers.
    
    Valid modes: Plain, Context and Verbose.
    
    If called without arguments, acts as a toggle.
    

    if you look carefully the 2 following example are different, but difference is more visible with long tracebacks :

    In [8]: raise ValueError('Foo')
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-8-05e81bf5c607> in <module>()
    ----> 1 raise ValueError('Foo')
            global ValueError = undefined
    
    ValueError: Foo
    

    Plain mode

    In [9]: xmode
    Exception reporting mode: Plain
    
    In [10]: raise ValueError('Foo')
    Traceback (most recent call last):
      File "<ipython-input-10-05e81bf5c607>", line 1, in <module>
        raise ValueError('Foo')
    ValueError: Foo