Questions tagged [traceback]

A traceback is a report of the actions leading up to a run-time error in the execution of a program.

A traceback is a report of the actions leading up to a run-time error in the execution of a program. For example, the following traceback:

Traceback (most recent call last):
  File "convert.py", line 23, in <module>
    display(literal_eval(sys.argv[1]), *sys.argv[2:])
  File "convert.py", line 16, in display
    result = convert(quantity, from_unit, to_unit)
  File "convert.py", line 10, in convert
    conversion = CONVERSIONS[from_unit, to_unit]
KeyError: ('cm', 'inch')

shows that in the Python program convert.py, the function display() was called, which in turn called the function convert(), which attempted to look up a nonexistent item with the key ('cm', 'inch') in the dictionary CONVERSIONS.

Reporting the actions leading up to an error, rather than just the line on which the error occurred, can assist in debugging a faulty program.

730 questions
1372
votes
19 answers

Catch and print full Python exception traceback without halting/exiting the program

I want to catch and log exceptions without exiting, e.g., try: do_stuff() except Exception as err: print(Exception, err) # I want to print the entire traceback here, # not just the exception name and details I want to print the…
chriscauley
  • 19,015
  • 9
  • 33
  • 33
720
votes
27 answers

Determine function name from within that function (without using traceback)

In Python, without using the traceback module, is there a way to determine a function's name from within that function? Say I have a module foo with a function bar. When executing foo.bar(), is there a way for bar to know bar's name? Or better…
Rob
  • 7,420
  • 3
  • 17
  • 12
393
votes
29 answers

Showing the stack trace from a running Python application

I have this Python application that gets stuck from time to time and I can't find out where. Is there any way to signal Python interpreter to show you the exact code that's running? Some kind of on-the-fly stacktrace? Related questions: Print…
Seb
  • 17,141
  • 7
  • 38
  • 27
392
votes
7 answers

When I catch an exception, how do I get the type, file, and line number?

Catching an exception that would print like this: Traceback (most recent call last): File "c:/tmp.py", line 1, in 4 / 0 ZeroDivisionError: integer division or modulo by zero I want to format it into: ZeroDivisonError, tmp.py, 1
Claudiu
  • 224,032
  • 165
  • 485
  • 680
307
votes
10 answers

How to exit from Python without traceback?

I would like to know how to I exit from Python without having an traceback dump on the output. I still want want to be able to return an error code but I do not want to display the traceback log. I want to be able to exit using exit(number) without…
sorin
  • 161,544
  • 178
  • 535
  • 806
103
votes
5 answers

Get Traceback of warnings

In numpy we can do np.seterr(invalid='raise') to get a traceback for warnings raising an error instead (see this post). Is there a general way for tracing warnings? Can I make python to give a traceback, when a warning is raised?
embert
  • 7,336
  • 10
  • 49
  • 78
76
votes
6 answers

Exception traceback is hidden if not re-raised immediately

I've got a piece of code similar to this: import sys def func1(): func2() def func2(): raise Exception('test error') def main(): err = None try: func1() except: err = sys.exc_info()[1] pass # some…
parxier
  • 3,811
  • 5
  • 42
  • 54
54
votes
7 answers

Print an error message without printing a traceback and close the program when a condition is not met

I've seen similar questions to this one but none of them really address the trackback. If I have a class like so class Stop_if_no_then(): def __init__(self, value one, operator, value_two, then, line_or_label, line_number): …
user2560035
  • 701
  • 2
  • 6
  • 12
52
votes
2 answers

What is the difference between a stack and a frame?

Under what situations would I want to use one over the other? What is the difference between: >>> import inspect >>> print(inspect.getouterframes(inspect.currentframe())) [(, '', 1, '', None,…
jmunsch
  • 22,771
  • 11
  • 93
  • 114
44
votes
3 answers

How to limit python traceback to specific files

I write a lot of Python code that uses external libraries. Frequently I will write a bug, and when I run the code I get a big long traceback in the Python console. 99.999999% of the time it's due to a coding error in my code, not because of a bug…
J-bob
  • 8,380
  • 11
  • 52
  • 85
43
votes
4 answers

How to get a complete exception stack trace in Python

The following snippet: import traceback def a(): b() def b(): try: c() except: traceback.print_exc() def c(): assert False a() Produces this output: Traceback (most recent call last): File "test.py", line 8, in…
Gordon Wrigley
  • 11,015
  • 10
  • 48
  • 62
40
votes
6 answers

Python: Getting a traceback from a multiprocessing.Process

I am trying to get hold of a traceback object from a multiprocessing.Process. Unfortunately passing the exception info through a pipe does not work because traceback objects can not be pickled: def foo(pipe_to_parent): try: raise…
knipknap
  • 5,934
  • 7
  • 39
  • 43
40
votes
2 answers

What are the python builtin __exit__ argument types?

Classes have a defineable function __exit__ that allows implementation of a context manager. It takes the required arguments: def __exit__(self, exc_type, exc_val, exc_tb): but I cannot find a definitive definition of what those arguments are and…
notacorn
  • 3,526
  • 4
  • 30
  • 60
36
votes
3 answers

How can I more easily suppress previous exceptions when I raise my own exception in response?

Consider try: import someProprietaryModule except ImportError: raise ImportError('It appears that is not installed...') When run, if someProprietaryModule is not installed, one sees: (traceback data) ImportError:…
Hammerite
  • 21,755
  • 6
  • 70
  • 91
36
votes
7 answers

How can I modify a Python traceback object when raising an exception?

I'm working on a Python library used by third-party developers to write extensions for our core application. I'd like to know if it's possible to modify the traceback when raising exceptions, so the last stack frame is the call to the library…
James Clarke
  • 553
  • 1
  • 5
  • 9
1
2 3
48 49