WEBVTT - This file was automatically generated by VIMEO

0
00:00:07.680 --> 00:00:12.060
Hi. In this video we will learn how to debug code in our notebook.

1
00:00:14.230 --> 00:00:18.700
Let's say that we coded something wrong and we need to find cause of this, uh,

2
00:00:18.800 --> 00:00:20.100
why our code doesn't work.

3
00:00:24.780 --> 00:00:27.090
Let's change in our last cell.

4
00:00:27.310 --> 00:00:31.840
The alpha variable we want to intentionally throw an

5
00:00:31.840 --> 00:00:32.673
exception

6
00:00:36.240 --> 00:00:40.340
in this case is the reason why our code doesn't work. Uh, obvious.

7
00:00:41.080 --> 00:00:43.740
We see that the problem is line number 25,

8
00:00:44.200 --> 00:00:49.110
and also we have marked alpha variable and we also got

9
00:00:49.110 --> 00:00:51.230
the name, error name. Aha is not defined,

10
00:00:54.160 --> 00:00:57.460
but when you have more code, it might be not so easy.

11
00:00:57.970 --> 00:00:59.780
This is where the debugger comes in.

12
00:01:03.260 --> 00:01:08.120
You can start the interactive debugger with the magic command debug.

13
00:01:13.020 --> 00:01:17.990
This will open an interactive debugger session with a prompt right at

14
00:01:17.990 --> 00:01:21.880
the line where the exception occurred was run it.

15
00:01:26.030 --> 00:01:28.970
And now we can, uh, examine the variables. For example,

16
00:01:39.980 --> 00:01:43.720
Python debugger has few commands, which helps you navigate, uh,

17
00:01:43.740 --> 00:01:45.480
but I will show you them in our next example.

18
00:01:45.860 --> 00:01:49.280
Now press Q and enter To create the debugger,

19
00:01:56.290 --> 00:02:01.230
we can use the python debugger without the debug magic. Uh, let's try that.

20
00:02:01.800 --> 00:02:02.710
Let's put the code,

21
00:02:02.760 --> 00:02:07.430
which fits our model and makes the predictions into a function which will take

22
00:02:07.430 --> 00:02:12.050
four parameters. The data frame name of the explanatory variable,

23
00:02:12.440 --> 00:02:16.810
name of the dependent variable, and the balloon variable if to fit,

24
00:02:16.810 --> 00:02:20.180
intercept or not. But first,

25
00:02:20.310 --> 00:02:23.820
let's create a new section called debugging,

26
00:02:29.010 --> 00:02:30.910
and let's import the debugger.

27
00:02:46.630 --> 00:02:47.720
This is our function

28
00:02:53.780 --> 00:02:57.890
and let's also use that function. We see it's working.

29
00:02:59.730 --> 00:03:01.680
Let's create an intentional error.

30
00:03:03.800 --> 00:03:05.460
We replace the X two I

31
00:03:09.220 --> 00:03:11.120
and we see we got an exception.

32
00:03:13.670 --> 00:03:16.690
And the problem is somewhere with the shape of the data.

33
00:03:18.110 --> 00:03:19.170
And let's debug it.

34
00:03:24.400 --> 00:03:29.370
Command p d b that traced. This is the place, uh,

35
00:03:29.500 --> 00:03:31.090
where our debugger starts.

36
00:03:36.120 --> 00:03:38.260
We see it here by this arrow.

37
00:03:42.210 --> 00:03:47.140
Now we can do a lot of things. Uh, we can, uh, print the variables.

38
00:03:52.130 --> 00:03:53.550
We can execute current line,

39
00:03:56.250 --> 00:03:57.710
we can list the code.

40
00:04:00.340 --> 00:04:04.680
Or with double L, we can list all code. In the current function,

41
00:04:08.070 --> 00:04:12.350
we can set break points. Let's set the break point at, uh,

42
00:04:12.780 --> 00:04:13.910
line number seven.

43
00:04:17.020 --> 00:04:18.520
We can list break points.

44
00:04:21.140 --> 00:04:22.600
We can clear the break points,

45
00:04:27.590 --> 00:04:28.810
but let's set it again.

46
00:04:32.550 --> 00:04:36.050
We can execute all code until the breakpoint.

47
00:04:42.040 --> 00:04:43.900
We can step into a function.

48
00:04:46.970 --> 00:04:51.910
Now we are in the predict function. We can go up in the stack trace.

49
00:04:56.400 --> 00:05:00.900
We can execute, uh, all code until current function returns.

50
00:05:08.080 --> 00:05:13.050
But here we have the error of course, which clear we quit the debugger.

51
00:05:16.440 --> 00:05:20.950
There are more, uh, commands and more shortcuts. Uh,

52
00:05:20.970 --> 00:05:24.870
please find the reference to the python debugger in the notebook.

53
00:05:28.420 --> 00:05:32.880
If you are curious, you can try also other debuggers, uh, for example,

54
00:05:32.910 --> 00:05:36.480
some visual debugger like pixie about, uh,

55
00:05:36.720 --> 00:05:38.440
I like the standard debugger the most.

56
00:05:41.420 --> 00:05:42.270
Unfortunately,

57
00:05:42.770 --> 00:05:47.510
the regular python debugger will not work using it outside of a function in the

58
00:05:47.670 --> 00:05:49.730
notebook. Let's see an example.

59
00:05:54.680 --> 00:05:58.120
If we'll put, uh, the p d b set trace. Here

60
00:06:05.630 --> 00:06:09.890
we go to the interactive debugger, but when we want to use it, it, uh,

61
00:06:09.920 --> 00:06:13.730
doesn't work properly. Basically using it, we'll throw an exception.

62
00:06:22.300 --> 00:06:25.160
In these cases, we have to use the debug magic.

63
00:06:28.030 --> 00:06:31.530
In our next video, we will learn how to share our notebooks.
