1414NB_VERSION = 4
1515
1616
17+ class CellImportError (Exception ):
18+ pass
19+
20+
1721class NotebookRun :
1822 filename : Path
1923 verbose : bool
@@ -24,11 +28,13 @@ def __init__(
2428 default_timeout : int ,
2529 verbose : bool = False ,
2630 kernel : Optional [str ] = None ,
31+ find_import_errors : bool = False ,
2732 ) -> None :
2833 self .filename = filename
2934 self .verbose = verbose
3035 self .default_timeout = default_timeout
3136 self .kernel = kernel
37+ self .find_import_errors = find_import_errors
3238
3339 def execute (
3440 self ,
@@ -56,7 +62,8 @@ def execute(
5662 c = NotebookClient (
5763 nb ,
5864 timeout = timeout ,
59- allow_errors = allow_errors ,
65+ allow_errors = allow_errors or self .find_import_errors ,
66+ interrupt_on_timeout = self .find_import_errors ,
6067 record_timing = True ,
6168 ** extra_kwargs ,
6269 )
@@ -68,6 +75,11 @@ async def apply_mocks(
6875 if any (o ["output_type" ] == "error" for o in cell ["outputs" ]):
6976 execute_reply ["content" ]["status" ] = "error"
7077
78+ if "ename" in execute_reply ["content" ]:
79+ if execute_reply ["content" ]["ename" ] == "ModuleNotFoundError" :
80+ if self .find_import_errors :
81+ raise CellImportError ()
82+
7183 if c .kc is None :
7284 raise Exception ("there is no kernelclient" )
7385 mocks : Dict [str , Any ] = (
@@ -85,6 +97,8 @@ async def apply_mocks(
8597 c .on_cell_executed = apply_mocks
8698
8799 c .execute (cwd = self .filename .parent )
100+ except CellImportError :
101+ error = self ._get_error (nb )
88102 except CellExecutionError :
89103 error = self ._get_error (nb )
90104 except CellTimeoutError as err :
0 commit comments