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?
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.
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.
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, 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.
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,
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.
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
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: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.
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
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.”
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.
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.
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.