Debugging
00:00 Okay! Let’s do some debugging in Jupyter Notebooks.
00:04 When I head over to our little code snippet in this Jupyter Notebook, when I run this, this is our output. And now we want to see how can we figure out if something went wrong in there. I’ll mainly show you two ways of doing this—one that is very simple and built-in, but it’s not as optically nice to look at and to do debugging with as it is in other editors, so I want to also show you an extension that you can get for Jupyter that makes debugging a bit nicer. Let’s start off with the simple built-in stuff. Okay, so let’s assume we have an error in here, right?
00:38
Simply this integer is missing, so when I run this, I’m getting an error invalid syntax
, it’s pointing me there. Already kind of like a nice debugger, but what you can do here in Jupyter is you can type this magic command, %debug
, right after you got an error, and then run it.
00:56 And this opens up the debugger and gives us the traceback, so we can read back up on what happened—why did this fail?—and it also opens up the interactive debugger.
01:06
So in here, I can type in pdb
commands, I can see what does x
evaluate to at the moment. Currently it’s not defined. So I can step to the next section.
01:18
Ha, and this is actually a bit annoying, yeah. So if you just use this %debug
like that, it kind of kicks you out of this. Another way to go about doing this is you can use that command with double percentage signs (%%
) at the beginning of the cell, and then even if there’s no traceback in there—like, using it in the next cell only works right after you got an error. But if you put this %%debug
at the beginning of the cell, then you can debug the cell no matter what. So, let’s take a look at that instead. If I run this, it opens up the interactive debugger for me.
01:53
And now in here, I can play around with it. So x
—because we actually had it run all the way—equals 16
.
02:00
Let’s see what the next one brings me. Now it’s starting from the beginning, so x
equals 0
, and I can step through this with n
, which is just a pdb
command.
02:11
And it goes line-by-line. So it’s a little annoying, you see. It’s not very beautiful. I have to step n
a couple of times until x
actually changes its value, and we don’t really get a nice optical overview of what’s going on. But it can still be helpful, and this command is already built-in with Jupyter. So, check out magic commands—you can Google that, and they have a nice documentation on different ones that are available. There’s one that uses pdb
, so %pdb
, which essentially just does something… Like, the pdb
is the default Python debugger, so we can obviously also just use this in there because we’re just working with Python, right? So I can import pdb
and then set_trace()
, which is setting a breakpoint.
02:53
Let’s see if it gives me an autocomplete. It does not! Okay, set_trace()
, like this—I can run that.
03:03 I just restart the kernel, so I start from a fresh slate.
03:12
Okay. Here we go. Okay, so I imported pdb
, the household Python debugger, and then set a breakpoint. This is what happens in some of the other ones we saw—we could nicely click at the edge and set a breakpoint like this. Here, if we’re just using this plain Python, we have to do it in this way. Okay, so I run this now, you see we’re entering pdb
, and again, I can do similar things as before. I can evaluate x
, I can say, “Okay, what’s happening when I step to the next line?” Now, x
equals 1
.
03:43 I can also continue, which hops over to the next breakpoint.
03:48
And then let’s see what x
is now. Still 1
, continue, x
is 4
. So, this gives you a way of inspecting it, which is just plain Python debugging.
04:00 It’s not very nice and not very easy to see what’s going on in here, so let’s look at the extension for Jupyter Notebooks that makes this whole thing a little bit easier. Okay. So as I mentioned, we’re also going to look at an extension that makes debugging a bit nicer in Jupyter Notebooks.
04:16
And this one that I’m going to show you here is called pixiedust
. Since it’s an extension, it’s external—it doesn’t come with Jupyter Notebooks, so you’ll have to install it, just using pip install
. And if you’re in the new environment, a new virtual environment, that doesn’t include these very common data science libraries, you’ll probably also have to install matplotlib
and pandas
.
04:36 Whoops. So here, you see a Markdown cell—I wrote this up in Markdown and it looks nice. Sweet, huh? Okay. So after you installed these three libraries, you can go ahead and…
04:50
I’m just making a new cell here. We’ll have to import it, so I say import pixiedust
.
04:59
Okay. So this worked. We got the install. And in case any of these are not installed at first, you will get an error here and it’s going to tell you, “Oh, matplotlib
is missing,” or “You don’t have this library installed.” But once all this is done, we can import it. And now at the beginning of a cell,
05:17
we can set another magic command, which is %%pixie_debugger
.
05:24 And if I run it now, it will open up this interactive debugger here. You see, this already looks much nicer than just having the text as we said before. And in here, you can use buttons that you might be familiar with from other editors.
05:40
And yeah, we can step through the code, so we can just keep going, and then here we see the variables that currently exist. So x
points to 0
at the moment.
05:51
We can keep going, step in, see the printout. This steps into some related function that we are not actually interested in, so we can also step out of this again, just continuing our for
loop—because that’s what we actually want to debug—and keep stepping. Okay, x
changes its value to 1
.
06:10 So as you can see, this is much more intuitive and easier to work with.
06:16
Okay, so if I step over, now we can see x
. We can see the variable value here changes, and it’s easy to see what’s going on in this window on the side, and we can also evaluate an expression. x
points to 2
, I can say, “What happens if I square this now?” And I can see the result coming up over here. And yes, you can also set breakpoints. For example, I can say “Let’s set a breakpoint in line 5.”
06:46
I can say just 5
, and we see a little bug symbol coming up here. This is similar to how it also works in other editors. I can also just click here to create this. We can see the breakpoints happening here, you can get rid of those if you’re not interested.
07:00
And so this interactive debugger, called pixie_debugger
, it gives you just a more intuitive way of debugging cells in Jupyter Notebooks. Again, though, it is an extension, so if you want to use this, you’re going to have to install it separately.
07:14
Otherwise, getting some familiarity with using pdb
, as we looked at before, can of course also be helpful. Cool! Okay, so those are two ways that you can debug in Jupyter Notebooks. You see, it’s maybe not as nice as it is in some of the other editors that we looked at, but it’s a very powerful tool and there’s also still a lot in development, so I’m sure a lot of other projects that I probably don’t know about are in development and are going to come out and make things even nicer than they are already. Sweet! So if you’re excited about Jupyter Notebooks, let’s take a look in the next and final video about how you can learn more about working with them. See you there!
Become a Member to join the conversation.