This page was generated from doc/allow-errors.ipynb. Interactive online version: Binder badge.

Ignoring Errors

Normally, if an exception is raised while executing a notebook, the Sphinx build process is stopped immediately.

If a notebook contains errors on purpose (or if you are too lazy to fix them right now), you have four options:

  1. Manually execute the notebook in question and save the results, see the pre-executed example notebook.

  2. Allow errors in all notebooks by setting this option in conf.py:

    nbsphinx_allow_errors = True
    
  3. Allow errors on a per-notebook basis by adding this to the notebook’s JSON metadata:

    "nbsphinx": {
      "allow_errors": true
    },
    
  4. Allow errors on a per-cell basis using the raises-exception tag, see Ignoring Errors on a Cell-by-Cell Basis.

This very notebook is an example for the third option. The results of the following code cells are not stored within the notebook, therefore it is executed during the Sphinx build process. Since the above-mentioned allow_errors flag is set in this notebook’s metadata, all cells are executed although most of them cause an exception.

[1]:
nonsense
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3398, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "/tmp/ipykernel_709621/3654466553.py", line 1, in <cell line: 1>
    nonsense
NameError: name 'nonsense' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 317, in executing
    args = executing_cache[key]
KeyError: (<code object run_code at 0x7fbc2a8b2f50, file "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3362>, 140446144343888, 74)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 1993, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1118, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1012, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 865, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 799, in format_exception_as_a_whole
    self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else []
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 854, in get_records
    return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:]
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 565, in stack_data
    yield from collapse_repeated(
  File "/usr/lib/python3.8/site-packages/stack_data/utils.py", line 84, in collapse_repeated
    yield from map(mapper, original_group)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 555, in mapper
    return cls(f, options)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 520, in __init__
    self.executing = Source.executing(frame_or_tb)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 372, in executing
    args = find(source=cls.for_frame(frame), retry_cache=True)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 252, in for_frame
    return cls.for_filename(frame.f_code.co_filename, frame.f_globals or {}, use_cache)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 270, in for_filename
    result = source_cache[filename] = cls._for_filename_and_lines(filename, lines)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 281, in _for_filename_and_lines
    result = source_cache[(filename, lines)] = cls(filename, lines)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 81, in __init__
    self.asttokens()
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 416, in asttokens
    return ASTTokens(
  File "/usr/lib/python3.8/site-packages/asttokens/asttokens.py", line 62, in __init__
    self.mark_tokens(self._tree)
  File "/usr/lib/python3.8/site-packages/asttokens/asttokens.py", line 73, in mark_tokens
    MarkTokens(self).visit_tree(root_node)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 47, in visit_tree
    util.visit_tree(node, self._visit_before_children, self._visit_after_children)
  File "/usr/lib/python3.8/site-packages/asttokens/util.py", line 184, in visit_tree
    ret = postvisit(current, par_value, value)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 90, in _visit_after_children
    nfirst, nlast = self._methods.get(self, node.__class__)(node, first, last)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 175, in visit_listcomp
    return self.handle_comp('[', node, first_token, last_token)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 171, in handle_comp
    util.expect_token(before, token.OP, open_brace)
  File "/usr/lib/python3.8/site-packages/asttokens/util.py", line 56, in expect_token
    raise ValueError("Expected token %s, got %s on line %s col %s" % (
ValueError: Expected token OP:'[', got OP:'+' on line 1325 col 73
[2]:
42 / 0
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3398, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "/tmp/ipykernel_709621/3599633117.py", line 1, in <cell line: 1>
    42 / 0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 317, in executing
    args = executing_cache[key]
KeyError: (<code object run_code at 0x7fbc2a8b2f50, file "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3362>, 140446144343888, 74)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 1993, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1118, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1012, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 865, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 799, in format_exception_as_a_whole
    self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else []
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 854, in get_records
    return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:]
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 565, in stack_data
    yield from collapse_repeated(
  File "/usr/lib/python3.8/site-packages/stack_data/utils.py", line 84, in collapse_repeated
    yield from map(mapper, original_group)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 555, in mapper
    return cls(f, options)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 520, in __init__
    self.executing = Source.executing(frame_or_tb)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 372, in executing
    args = find(source=cls.for_frame(frame), retry_cache=True)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 252, in for_frame
    return cls.for_filename(frame.f_code.co_filename, frame.f_globals or {}, use_cache)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 270, in for_filename
    result = source_cache[filename] = cls._for_filename_and_lines(filename, lines)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 281, in _for_filename_and_lines
    result = source_cache[(filename, lines)] = cls(filename, lines)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 81, in __init__
    self.asttokens()
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 416, in asttokens
    return ASTTokens(
  File "/usr/lib/python3.8/site-packages/asttokens/asttokens.py", line 62, in __init__
    self.mark_tokens(self._tree)
  File "/usr/lib/python3.8/site-packages/asttokens/asttokens.py", line 73, in mark_tokens
    MarkTokens(self).visit_tree(root_node)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 47, in visit_tree
    util.visit_tree(node, self._visit_before_children, self._visit_after_children)
  File "/usr/lib/python3.8/site-packages/asttokens/util.py", line 184, in visit_tree
    ret = postvisit(current, par_value, value)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 90, in _visit_after_children
    nfirst, nlast = self._methods.get(self, node.__class__)(node, first, last)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 175, in visit_listcomp
    return self.handle_comp('[', node, first_token, last_token)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 171, in handle_comp
    util.expect_token(before, token.OP, open_brace)
  File "/usr/lib/python3.8/site-packages/asttokens/util.py", line 56, in expect_token
    raise ValueError("Expected token %s, got %s on line %s col %s" % (
ValueError: Expected token OP:'[', got OP:'+' on line 1325 col 73
[3]:
print 'Hello, world!'
  Input In [3]
    print 'Hello, world!'
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello, world!')?

[4]:
6 ~ 7
  Input In [4]
    6 ~ 7
      ^
SyntaxError: invalid syntax

[5]:
6 * 7
[5]:
42