Hi. In this video we will learn how to debug code in our notebook.
Let's say that we coded something wrong and we need to find cause of this, uh,
why our code doesn't work.
Let's change in our last cell.
The alpha variable we want to intentionally throw an
exception
in this case is the reason why our code doesn't work. Uh, obvious.
We see that the problem is line number 25,
and also we have marked alpha variable and we also got
the name, error name. Aha is not defined,
but when you have more code, it might be not so easy.
This is where the debugger comes in.
You can start the interactive debugger with the magic command debug.
This will open an interactive debugger session with a prompt right at
the line where the exception occurred was run it.
And now we can, uh, examine the variables. For example,
Python debugger has few commands, which helps you navigate, uh,
but I will show you them in our next example.
Now press Q and enter To create the debugger,
we can use the python debugger without the debug magic. Uh, let's try that.
Let's put the code,
which fits our model and makes the predictions into a function which will take
four parameters. The data frame name of the explanatory variable,
name of the dependent variable, and the balloon variable if to fit,
intercept or not. But first,
let's create a new section called debugging,
and let's import the debugger.
This is our function
and let's also use that function. We see it's working.
Let's create an intentional error.
We replace the X two I
and we see we got an exception.
And the problem is somewhere with the shape of the data.
And let's debug it.
Command p d b that traced. This is the place, uh,
where our debugger starts.
We see it here by this arrow.
Now we can do a lot of things. Uh, we can, uh, print the variables.
We can execute current line,
we can list the code.
Or with double L, we can list all code. In the current function,
we can set break points. Let's set the break point at, uh,
line number seven.
We can list break points.
We can clear the break points,
but let's set it again.
We can execute all code until the breakpoint.
We can step into a function.
Now we are in the predict function. We can go up in the stack trace.
We can execute, uh, all code until current function returns.
But here we have the error of course, which clear we quit the debugger.
There are more, uh, commands and more shortcuts. Uh,
please find the reference to the python debugger in the notebook.
If you are curious, you can try also other debuggers, uh, for example,
some visual debugger like pixie about, uh,
I like the standard debugger the most.
Unfortunately,
the regular python debugger will not work using it outside of a function in the
notebook. Let's see an example.
If we'll put, uh, the p d b set trace. Here
we go to the interactive debugger, but when we want to use it, it, uh,
doesn't work properly. Basically using it, we'll throw an exception.
In these cases, we have to use the debug magic.
In our next video, we will learn how to share our notebooks.