Introduction to the Python Debugger

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey there welcome to my first ever live stream where I'm going to be showing you how to get started using the Python debugger tool the Python debugger tool or PDB is a tool that will save you an incredible amount of time it's really useful and when I used to debug my Python applications when I first got started, if there was a problem and I wanted to know what kind of variable was set to what value I would just simply print that out on the screen by literally putting print commands in my code and I would get to the point where I would have to put print with tons of equal signs or something like that so I could actually see the output amongst all of the logs of the code that I was running so I could actually identify where my output was so I could inspect which variable was set to which value this all changed when I discovered the Python debugger tool it saves me so much time and I use it all day pretty much every day when I'm debugging my code anytime I have any issue with my code or I want to see what's going on I use the Python debugger tool and it helps me figure out how to fix the bug so I'm going to be showing you how to use the Python debugger tool with a very simple Python project that I've created and shared on github so if you look at the notes of this video you can see a link to the demo JSON printer project now this is a very simple project that I created that only contains two files that's actually I'll show you on the github page first so if you want to find it it's under LondonAppDev demo-JSON-printer and it just contains these two files the hello debugger.py file and the sample.json file all it is is a very basic script in Python that we run using the command line and what it should do is it should output the values of the sample.json file it should output the keys and the values next to each other but there's a couple of bugs that I've deliberately put into this code and it currently doesn't work so when you clone the project if you type Python hello debugger you can see that it gives this attribute error non-type object has no attribute items now it's a pretty basic issue pretty simple bug so you'll probably be able to figure it out pretty easily just by looking at the code but I just wanted to create a simple script that I could use to demonstrate the Python debugger tool and show you how to use it so what you do with the Python debugger tool is you basically set a line in the code which will stop the execution and allow you to manually step forward or inspect the different values in your code it's a bit similar to breakpoints if you've ever developed a java application or like an android application or something you can set breakpoints in the code so that when the execution gets to that specific breakpoint or the line that you've set the breakpoint on it will stop the execution and give you an interactive debugger access to that area of the code so you can do that using the python debugger tool the way that you use it is you simply set use pdb.set_trace but first you need to import the Python debugger so I'm just going to do it at the beginning of this main function here so you can see that when you run the command when you run the script it runs this main function and I'm going to set the breakpoint right here at the beginning so we can start debugging from the beginning type import pdb; pdb.set_trace now this is not good pythonic code right here you would never put this line of code in your actual in your actual source code and you should never import and run a command on the same line the reason why I do it for the python debugger is I don't want to accidentally leave the Python debugger line in my code and if you have a good linter tool or something enabled which you should have on your Python project then it will warn me about this line being there because I've put the import and the set trace on the same line so because it's invalid Python code it should warn me my Python linter should warn me that this is invalid so it just helps me not forget that that line is there so I don't know deploying it and then it ends up breaking the application on the live environment because there's a debugger set trace set there but anyway this is how you set the debugger up to work so now if we go back to our terminal and we try and run our script you'll notice that this time it stops the execution exactly where we set this import line here so it stops it on this line 18 here and this here above the PDB prompt is the next line that it's going to execute so there's lots of things you could do with the Python debugger and it can be quite complicated but there's really only three main things that you need to know or three main Python debugger commands that you need to know in order to use it effectively and to get the most use out of it I'm just going to keep it really simple I'm just going to show you the three basic options that will help you debug your applications really quickly and figure out what's going on so the first command Python debugger command is the next command now the next command simply moves to the next line in the execution so we're currently on this, we're currently here and the next line is here so if we type next and hit enter it will simply execute the next line of code if we type it again then it should give us the attribute error that we saw earlier because now it's executed this line and this is where we're getting the error we're getting the error in here so it's saying that this is a non type and non type has no attribute items so if we hit next again and we get next again it will eventually take us to the end of our code and the execution will exit if you just hit enter then it will just run the last command that you ran so you can just hold that down until the end of the code and there you go so it ended up eventually exiting I'm just going to clear that now if I run the script again it will load and it will again stop because the Python debugger set trace is still there we'll stop there and I can show you the next command which is not the next command but I just showed you the "next command" which lets you step through the execution. The other Python debugger command that's really useful is the "step command" the step well instead of moving to the next line it will step into the next line so for example if I type step here then it will actually step into this function so it won't just go to the next line in this... it won't go to the next line in this function it will step into the function that we're actually calling so this gives you a bit more depth into the code so if we wanted to look up what's going on in here we can do step and then it says that the next line is the path to a dict function we type step again you can see it's moved to the with open if we type here print and at this point you can execute any Python code that you want so if you want to manually see what this path argument is set to you just type print whoops path hit enter and it tells us what the value of this is so this is the real value of the python debugger you can go into the different variables and see during the execution what they are set to so you know where the issue lies so we can see that this path argument is set to the correct value sample.json because it's being passed in here so now let's show you the third command is the "continue command" now continue, what that does is it will continue to either the end of the execution or the next python debugger set trace command so if we type continue you can see that it continues the execution and goes right to the end if we were to leave this line here but add another Python debugger line in our print values for loop, let's type import pdb pdb.set trace save that and then run the command again so it stops at the first Python debugger if we type continue you can actually shorten this to just cont or maybe not okay so continue will run to the end of the code still even though we have importer here let's try import pdb up here and then try this nope continue runs to the end of the execution so if we type next print values next... okay so we still get the arrow there so now let's try and step into this print values command and so we're gonna move the Python set trace let's remove it from there and type run the command again okay so now it's stopped stop at the beginning still I guess it didn't save and continue okay so for some reason it's not reach...OK that's why because it's not reaching this because this isn't being executed because this is where the error is so I should have put the pdb set trace above this here and then if I go back and add pdb.set trace to the main I'm going to run that so it's going to stop here and I'm going to click type continue again and then it's going to continue to the next set trace which is here so now we're here so now we can do print input_dict and you can see this is set to none which is why we're getting the error and the reason this is set to none is because this function here is not returning the value that it should so if we were to add a return statement here and now we'll continue because we've just saved the code we need to run it again so we are stopped here right now so we're going to go continue and then this moves us to this point here line 14 so now we can print the input dict and you can see that it's actually got our dictionary of the contents of our sample .json file here and now if we type continue it will take us to the end of the code and you can see that it has successfully executed this time because it's actually output all the values which is what the code is meant to do so that's a simple usage of the PDB of the Python debugger let's say we wanted to move this into a for loop so that's another thing that continue will continue will execute until either the end of the code or the next set trace and that includes if the set trace is in a for loop as well so if there's multiple loops that it still has to do and we put and we type continue it will continue to loop around in those loops until the end of that iteration or until the end of all of the iterations basically so now I have this pdb.set trace here in the for loop let's try and run the code again and so it stopped us here so let's type continue it's looped around again and it started from the beginning of the loop so now lets type print k and you can see it's printed the key which is currently on name, I believe that is the second one if we go continue again print(k) it's on date and so on so you can continue all the way and eventually it will end the execution so that's how you use the Python debugger hopefully you can see the value in it and how it could save you quite a lot of time when you're debugging your applications so the only thing to remember is that when you're using the debugger again you must make sure that you remove any debug set trace lines at the end and don't commit it to your actual source code because if that runs in a live environment then sometimes it could you know completely crash the code and it will just stop your application working because it will it will freeze the execution during the live thread which will create problems so you don't want that. OK so that's how you use the Python debugger I hope you found this video useful if you have any questions or comments please leave them in the comments below and yeah please subscribe to the channel to get updates or get notifications when I release future videos like this thank you for watching.
Info
Channel: London App Developer
Views: 63,995
Rating: undefined out of 5
Keywords: python, debugger, debugging, python debugger, python3, how to code, software development
Id: 7Vmik1M_ry0
Channel Id: undefined
Length: 15min 19sec (919 seconds)
Published: Sat Sep 02 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.