traceback - Extract, format and print information about Python stack traces.
| Use Case | Command | Description |
|---|---|---|
| ๐ค Print current exception | print_exc() | Shortcut to print exception info from sys.exc_info() |
| ๐ Get exception as string | format_exc() | Like print_exc() but returns a string |
| ๐ Extract stack frames | extract_tb(tb) | Return StackSummary of pre-processed entries from traceback |
| ๐ Format stack list | format_list(list) | Format a list of FrameSummary objects for printing |
| ๐จ๏ธ Print stack trace | print_stack() | Print a stack trace from current invocation point |
| ๐ Extract current stack | extract_stack() | Extract raw traceback from current stack frame |
| โ ๏ธ Format exception with chain | format_exception(exc, value, tb) | Format stack trace and exception information |
| ๐งน Clear frame locals | clear_frames(tb) | Clear all references to local variables in traceback frames |
https://docs.python.org/3.10/library/traceback.html
The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.
builtins.list(builtins.object)
StackSummary
builtins.object
FrameSummary
TracebackException
class FrameSummary(builtins.object)
FrameSummary(filename, lineno, name, *, lookup_line=True, locals=None, line=None)
A single frame from a traceback.
None if locals were not supplied, or a dict mapping the name to the repr() of the variable.__eq__(self, other) โ Return self==value.__getitem__(self, pos)__init__(self, filename, lineno, name, *, lookup_line=True, locals=None, line=None) โ Construct a FrameSummary.
True, linecache is consulted for the source code line. Otherwise, the line will be looked up when first needed.__iter__(self)__len__(self)__repr__(self) โ Return repr(self).linefilenamelinenolocalsname__hash__ = Noneclass StackSummary(builtins.list)
StackSummary(iterable=(), /)
A stack of frames.
Method resolution order:
format(self) โ Format the stack ready for printing. Returns a list of strings ready for printing. Each string ends in a newline; may contain internal newlines. For long sequences of the same frame and line, shows first few repetitions and a summary line.extract(frame_gen, *, limit=None, lookup_lines=True, capture_locals=False) โ Create a StackSummary from a traceback or stack object.
None to include all frames or the number of frames to include.True, lookup lines for each frame immediately, otherwise lookup is deferred until the frame is rendered.True, the local variables from each frame will be captured as object representations into the FrameSummary.from_list(a_list) โ Create a StackSummary object from a supplied list of FrameSummary objects or old-style list of tuples.__dict__ โ dictionary for instance variables (if defined)__weakref__ โ list of weak references to the object (if defined)__add__(self, value, /) โ Return self+value.__contains__(self, key, /) โ Return key in self.__delitem__(self, key, /) โ Delete self[key].__eq__(self, value, /) โ Return self==value.__ge__(self, value, /) โ Return self>=value.__getattribute__(self, name, /) โ Return getattr(self, name).__getitem__(...) โ x.__getitem__(y) <==> x[y]__gt__(self, value, /) โ Return self>value.__iadd__(self, value, /) โ Implement self+=value.__imul__(self, value, /) โ Implement self*=value.__init__(self, /, *args, **kwargs) โ Initialize self. See help(type(self)) for accurate signature.__iter__(self, /) โ Implement iter(self).__le__(self, value, /) โ Return self<=value.__len__(self, /) โ Return len(self).__lt__(self, value, /) โ Return self<value.__mul__(self, value, /) โ Return self*value.__ne__(self, value, /) โ Return self!=value.__repr__(self, /) โ Return repr(self).__reversed__(self, /) โ Return a reverse iterator over the list.__rmul__(self, value, /) โ Return value*self.__setitem__(self, key, value, /) โ Set self[key] to value.__sizeof__(self, /) โ Return the size of the list in memory, in bytes.append(self, object, /) โ Append object to the end of the list.clear(self, /) โ Remove all items from list.copy(self, /) โ Return a shallow copy of the list.count(self, value, /) โ Return number of occurrences of value.extend(self, iterable, /) โ Extend list by appending elements from the iterable.index(self, value, start=0, stop=9223372036854775807, /) โ Return first index of value. Raises ValueError if the value is not present.insert(self, index, object, /) โ Insert object before index.pop(self, index=-1, /) โ Remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.remove(self, value, /) โ Remove first occurrence of value. Raises ValueError if the value is not present.reverse(self, /) โ Reverse *IN PLACE*.sort(self, /, *, key=None, reverse=False) โ Sort the list in ascending order and return None. The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained). If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values. The reverse flag can be set to sort in descending order.__class_getitem__(...) from builtins.type โ See PEP 585__new__(*args, **kwargs) from builtins.type โ Create and return a new object. See help(type) for accurate signature.__hash__ = Noneclass TracebackException(builtins.object)
TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False, compact=False, _seen=None)
An exception ready for rendering.
The traceback module captures enough attributes from the original exception to this intermediary form to ensure that no references are held, while still being able to fully print or format it.
Use from_exception to create TracebackException instances from exception objects, or the constructor to create TracebackException instances from individual components.
TracebackException of the original __cause__.TracebackException of the original __context__.StackSummary representing the traceback.None if not present.None if not present.__eq__(self, other) โ Return self==value.__init__(self, exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False, compact=False, _seen=None) โ Initialize self. See help(type(self)) for accurate signature.__str__(self) โ Return str(self).format(self, *, chain=True) โ Format the exception. If chain is not True, __cause__ and __context__ will not be formatted. The return value is a generator of strings, each ending in a newline and some containing internal newlines. print_exception is a wrapper around this method which just prints the lines to a file. The message indicating which exception occurred is always the last string in the output.format_exception_only(self) โ Format the exception part of the traceback. The return value is a generator of strings, each ending in a newline. Normally, the generator emits a single string; however, for SyntaxError exceptions, it emits several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is always the last string in the output.from_exception(exc, *args, **kwargs) from builtins.type โ Create a TracebackException from an exception.__dict__ โ dictionary for instance variables (if defined)__weakref__ โ list of weak references to the object (if defined)__hash__ = Noneclear_frames(tb)Clear all references to local variables in the frames of a traceback.
extract_stack(f=None, limit=None)Extract the raw traceback from the current stack frame. The return value has the same format as for extract_tb(). The optional f and limit arguments have the same meaning as for print_stack(). Each item in the list is a quadruple (filename, line number, function name, text), and the entries are in order from oldest to newest stack frame.
extract_tb(tb, limit=None)Return a StackSummary object representing a list of pre-processed entries from traceback. This is useful for alternate formatting of stack traces. If limit is omitted or None, all entries are extracted. A pre-processed stack trace entry is a FrameSummary object containing attributes filename, lineno, name, and line representing the information that is usually printed for a stack trace. The line is a string with leading and trailing whitespace stripped; if the source is not available it is None.
format_exc(limit=None, chain=True)Like print_exc() but return a string.
format_exception(exc, /, value=<implicit>, tb=<implicit>, limit=None, chain=True)Format a stack trace and the exception information. The arguments have the same meaning as the corresponding arguments to print_exception(). The return value is a list of strings, each ending in a newline and some containing internal newlines. When these lines are concatenated and printed, exactly the same text is printed as does print_exception().
format_exception_only(exc, /, value=<implicit>)Format the exception part of a traceback. The return value is a list of strings, each ending in a newline. Normally, the list contains a single string; however, for SyntaxError exceptions, it contains several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is always the last string in the list.
format_list(extracted_list)Format a list of tuples or FrameSummary objects for printing. Given a list of tuples or FrameSummary objects as returned by extract_tb() or extract_stack(), return a list of strings ready for printing. Each string in the resulting list corresponds to the item with the same index in the argument list. Each string ends in a newline; the strings may contain internal newlines as well, for those items whose source text line is not None.
format_stack(f=None, limit=None)Shorthand for format_list(extract_stack(f, limit)).
format_tb(tb, limit=None)A shorthand for format_list(extract_tb(tb, limit)).
print_exc(limit=None, file=None, chain=True)Shorthand for print_exception(*sys.exc_info(), limit, file).
print_exception(exc, /, value=<implicit>, tb=<implicit>, limit=None, file=None, chain=True)Print exception up to limit stack trace entries from tb to file. This differs from print_tb() in the following ways: (1) if traceback is not None, it prints a header "Traceback (most recent call last):"; (2) it prints the exception type and value after the stack trace; (3) if type is SyntaxError and value has the appropriate format, it prints the line where the syntax error occurred with a caret on the next line indicating the approximate position of the error.
print_last(limit=None, file=None, chain=True)This is a shorthand for print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file).
print_stack(f=None, limit=None, file=None)Print a stack trace from its invocation point. The optional f argument can be used to specify an alternate stack frame at which to start. The optional limit and file arguments have the same meaning as for print_exception().
print_tb(tb, limit=None, file=None)Print up to limit stack trace entries from the traceback tb. If limit is omitted or None, all entries are printed. If file is omitted or None, the output goes to sys.stderr; otherwise file should be an open file or file-like object with a write() method.
walk_stack(f)Walk a stack yielding the frame and line number for each frame. This will follow f.f_back from the given frame. If no frame is given, the current stack is used. Usually used with StackSummary.extract.
walk_tb(tb)Walk a traceback yielding the frame and line number for each frame. This will follow tb.tb_next (and thus is in the opposite order to walk_stack). Usually used with StackSummary.extract.
__all__ = ['extract_stack', 'extract_tb', 'format_exception', 'format_...']
/usr/lib/python3.10/traceback.py
Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-02 22:07 @216.73.216.239
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)