Visual Studio Code for Python Developers Transcripts
Chapter: Running and Debugging
Lecture: Command-line Debugging Techniques

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Before we dive into learning how we can run and debug our Python applications inside a Visual Studio Code using a Python extension,
0:08 first, I wanted us to take a moment to look at some of the existing command line debugging techniques that are available.
0:13 I mainly wanted to do this because for folks that might be coming from command line editors like VI or Emacs,
0:19 I wanted you all to see exactly how you could easily transition over these skills and how they work inside of the Visual Studio Code editor.
0:27 One thing you might have noticed so far in this course is that whenever we run our Python sample application, we do so by calling the Python command.
0:35 Now, what I'm showing you here is that I'm using the version of the command that's inside of our virtual environment. I'm going to clear the prompt.
0:42 Now, if I actually want to run our sample application, I could do something like this. I can call Python, I can pass it the -m switch,
0:50 then I'm going to give that switch the name of our module, which happens to be Podcast API. Now, when I run it, I'm going to give it a second,
0:57 and soon we'll see some output printed to the terminal, which will let us know that our application is doing the things that we expect it to.
1:04 Now, one thing a lot of developers do, and I know almost all of us have done it, whenever we need to get some more information about
1:12 what exactly is happening inside of our app, we use something called print line debugging. What that means is that we insert
1:19 print line statements throughout very key points of our application, and then we run it, and then we see what happens.
1:25 Now, for me personally, I'm not a big fan of print line debugging. I instead prefer to do something like logging.
1:31 So as an example, I'll do something like this, logger.info, and I'll set up a Python logger,
1:36 so it'll output to the screen with almost the same amount of information, but instead, I'll have a little bit more control over what goes out there.
1:44 One of the key benefits between logging and print line statements is the fact, I can specify the severity of the message that I want to output,
1:51 and also I can turn it on or off using configuration. So that way now whenever it gets into production, I don't have to worry about it too much.
1:57 If you take a look at the application, you'll see that it's not just a simple logger, but it's a very simple logger.
1:57 If you take a look at the terminal window right now, we can see we have various log messages such as debugger info,
2:03 along with the information associated with those particular logs. Now, if you're curious to see what our logging implementation looks like,
2:11 you can open the logging.py module inside of our project. Inside here, you could see that we're importing the built-in logging module from Python,
2:19 and along with some other things. But essentially, what I'm doing is creating a named logger called podcast API, setting the default level,
2:27 a custom formatter, and also the stream handler. If we head back over to our categories router at the top,
2:34 you can see that now I can just import that module and use it across all of our different parts of our application.
2:40 Now, if I go back over to the terminal, you can see at the bottom those two custom debug statements that are there. Those examples of log messages from
2:48 the custom logger that we set up inside of this project. Now that our sample application is up and running,
2:53 the next thing that we want to do is make a call to one of our API endpoints.
2:57 Now, you saw how to do this earlier using something called the Swagger UI. This is built-in functionality inside of our FastAPI project,
3:03 which is great because that means that whenever you create a new project, there's not a lot of extra work you have to do to get this setup.
3:10 But we are going to do something a little bit different now. Back inside of Visual Studio Code, I'm going to head over to the extension section,
3:17 and I'm going to search for something called the rest client. I'm going to select this one.
3:22 What this is, is an extension for Visual Studio Code that allows you to create custom HTTP calls.
3:28 Now, I really love this extension because it allows me to make calls to my API without having to switch between different windows,
3:34 and editors, and different tools. I can stay focused right here where I am inside of VS Code. Now, I'm going to head over to our Explorer,
3:42 and I'm going to create a new file. I'm just going to call this sample.http. Now, these.http files are supported by that rest client extension,
3:51 and they do have a very specific syntax. Now, I don't want to make a get request. The first thing I need to do is, well, I'm going to type get.
3:58 Notice how it has some really nice IntelliSense that really pops up for us. The next thing I need to do is give it the URL.
4:05 So I'm going to copy this URL here from the command line. I'm going to paste it in, and I'm going to give it the endpoint I actually want us to call,
4:11 which is going to be categories. Again, notice how that IntelliSense popped up. It actually overwrote some of my stuff.
4:19 That's not what I wanted to do, but let's do this. There we go. Categories. Now, I can hit the send request, and I can see that it's already done.
4:27 I can see that it's already done. I can see that it's already done. I can see that it's already done. I can see that it's already done.
4:32 I can see that it's already done. I can see that it's already done. I can see that it's already done. I can see that it's already done.
4:36 I can see that it's already done. I can see that it's already done. I can see that it's already done. I can see that it's already done.
4:40 I can see that it's already done. I can see that it's already done. I can see that it's already done. I can see that it's already done.
4:44 I can see that it's already done. I can see that it's already done. But still, we're able to stay inside of Visual Studio Code
4:49 without moving around too much and having a contact switch. Another helpful feature is you have the ability
4:54 to have more than one HTTP request inside of this file. So let's say I wanted to make another endpoint call. I'm going to copy this.
5:01 And let's say I wanted to make a call to the-- I think there's a shows endpoint. So I should be able to do something like categories.
5:09 Give it a category. I'm going to use art from the right side. And then I'm going to do /shows. There we go, shows. And let me close that.
5:16 I'm going to close this right now. Here we go. Right. Right now, notice I have two different requests inside of the same file.
5:23 So if I make this call, there you go, art and culture. I'm able to see the shows represented from the categories.
5:30 But the important part is, as you can see, I can have multiple API endpoints or multiple HTTP requests specified in this file.
5:37 I can even add things like headers or a data body or anything like that that I want to. And now I can save this file.
5:44 I can check it into source control if I needed to. And now different folks in my team are able to use it to debug our application.
5:51 Now, if you quickly take a look at the bottom of your screen, inside of the built-in terminal, you'll
5:55 notice that we got lock messages for every request that we made to our web API. So that means that we're able to see exactly what's
6:02 happening with our application as it's running using the logging that we set up. Now, I'm going to stop our application from running
6:08 and clear the terminal really quick. The next thing that we want to do is elevate this and take everything that we've seen so far
6:14 and use it with Python's built-in debugger. Now, Python itself comes with a debugger called PDB.
6:21 And the only thing that we need to do is give it the name of our module that we want to debug. In our case, it's going to be our podcast API.
6:29 I'm going to go ahead and hit Enter. And what's happening now is that it's actually started to run our application.
6:34 Now it's just stopped at the first line. But if I type in continue, notice that it's continued
6:42 to run the rest of our application, which is pretty cool. So we actually have a debugger now that we can use to not only run our applications,
6:49 but we can set things like breakpoints to actually run it. Why don't we try it out really quick? I'm going to stop our application again.
6:56 I'm going to type in exit to exit the debugger, and I'm going to clear the screen. Let's head back over to our Categories API.
7:05 We'll close this window, head back to Categories. Let's clear some of these things away from the screen. It's starting to get a little busy.
7:11 All right, let's put it here. And then let's also replace this with something that we can use to do some logging. Logger.info, right? There we go.
7:23 Do we have any more of those in there? Great. Logger.info. Now, the next thing that I want to do is I want to add a breakpoint.
7:29 And we can do that by calling back-- there's an actual breakpoint function built into Python. Let's go back to the terminal and run our application
7:37 with the debugger. OK, application running. I'm going to hit Continue. And I'm going to run it. And let's see what happens.
7:49 And you'll notice at the command line at the bottom, it says that it is now stopped at the Logger.info line of code.
7:55 It hasn't run it yet, but this is the next line of code that it's actually going to run. And if we head back over to the Categories router,
8:02 we can see exactly where that is. Now, inside of this debugging session, we can use all of the commands that we can expect to have,
8:10 like move on to the next statement, continue running, and so on. So we can see exactly what that is. And now, we can see that it's running.
8:18 We can even take a look at some of the variables that exist inside of this context right now. So let's say I typed in Logger.
8:24 And let's say I wanted to print the Logger. Right? You can see that it says we have an instance of Logger. The name Logger is Podcast API.
8:33 And it's set to debug right now. So again, we can kind of inspect the context of what's happening around this space.
8:40 If you wanted it to continue running, we'll just type Continue. And there you have it.
8:46 So we're done with the debugger from that REST Client API extension. We also get our log messages at the bottom.
8:52 And we're also still invoking using the built-in debugger from Python. Now, I'm not going to go super deep into all of the different things
8:59 you can do with the debugger. But the point of this was really just to show you whether you're coming from a command line tool or not,
9:06 you're able to use some of those familiar techniques inside of Visual Studio Code along with some other extensions that can make
9:11 your workflow just a little bit nicer.


Talk Python's Mastodon Michael Kennedy's Mastodon