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

1

8

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)
Ivatts answered 20/11, 2013 at 5:12 Comment(7)
I don't know IPython, but why is this a problem? The error has all information you needed.Disruption
Can you elaborate a bit on the if __name__ == "__main__" bit? Is the function defined in that block? If so, I'm not sure how your function call even worked at all. Can you give a SSCCE of the module?Arium
The error does not have all the information I need. For example, suppose there are many join() calls in function. The traceback does not tell me which one raised the error. This is unusual; the traceback would normally point me to the line in the source code that raised the error. Something is different about this situation, and I'm not sure what it is.Ivatts
Based on your description, I'm not able to reproduce the error. I made module.py with a function that simply raises a TypeError, and a if __name__ == "__main__": block that just has a print statement. Does this produce the error for you? If not, can you come up with a minimal example to reproduce the error? If so, can you provide some details of your setup (Python version, Ipython version, etc.)?Nuclei
No, the function is not defined in the if __name__ == '__main__' block. That would be outrageous. Furthermore, in my question I state that this block is "underneath" the function. To clarify, the if __name__ == '__main__' block is below the completed function definition, below the return statement.Ivatts
Sorry guys, I'm having a lot of trouble creating a minimal example that replicates the issue. And weirdly, this issue is not happening when I call the file as a script, and have a call to function in the if __name__ == '__main__' block. I'll report back as soon as I'm able to come up with a helpful example.Ivatts
there may be a difference in __builtin__ when you run the modulle / import it.Southerland
S
7

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
Scrapple answered 20/11, 2013 at 16:39 Comment(2)
That solved the problem to me. Nevertheless, the 'Plain' xmode of Ipython does not present all the traceback expected when using the Python3 'raise ... from' format. It only works well on the 'Context' and 'Verbose' modes. 'Verbose' mode is an overkill, by the way.Puritan
i haven't tried this, but since it worked for @idichekop, i've accepted it.Ivatts

© 2022 - 2024 — McMap. All rights reserved.