14
14
NB_VERSION = 4
15
15
16
16
17
+ class CellImportError (Exception ):
18
+ pass
19
+
20
+
17
21
class NotebookRun :
18
22
filename : Path
19
23
verbose : bool
@@ -24,11 +28,13 @@ def __init__(
24
28
default_timeout : int ,
25
29
verbose : bool = False ,
26
30
kernel : Optional [str ] = None ,
31
+ find_import_errors : bool = False ,
27
32
) -> None :
28
33
self .filename = filename
29
34
self .verbose = verbose
30
35
self .default_timeout = default_timeout
31
36
self .kernel = kernel
37
+ self .find_import_errors = find_import_errors
32
38
33
39
def execute (
34
40
self ,
@@ -56,7 +62,8 @@ def execute(
56
62
c = NotebookClient (
57
63
nb ,
58
64
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 ,
60
67
record_timing = True ,
61
68
** extra_kwargs ,
62
69
)
@@ -68,6 +75,11 @@ async def apply_mocks(
68
75
if any (o ["output_type" ] == "error" for o in cell ["outputs" ]):
69
76
execute_reply ["content" ]["status" ] = "error"
70
77
78
+ if "ename" in execute_reply ["content" ]:
79
+ if execute_reply ["content" ]["ename" ] == "ModuleNotFoundError" :
80
+ if self .find_import_errors :
81
+ raise CellImportError ()
82
+
71
83
if c .kc is None :
72
84
raise Exception ("there is no kernelclient" )
73
85
mocks : Dict [str , Any ] = (
@@ -85,6 +97,8 @@ async def apply_mocks(
85
97
c .on_cell_executed = apply_mocks
86
98
87
99
c .execute (cwd = self .filename .parent )
100
+ except CellImportError :
101
+ error = self ._get_error (nb )
88
102
except CellExecutionError :
89
103
error = self ._get_error (nb )
90
104
except CellTimeoutError as err :
0 commit comments