Visual Studio Code for Python Developers Transcripts
Chapter: Running and Debugging
Lecture: Working with Debug Configurations
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's see how we can create our first launch configuration. First thing we're going to do is open up the Explorer. Inside of that.VSCode folder,
0:08
you'll notice that there's no launch.JSON file. That means that we don't have any configurations that are set up as yet. Let's go ahead and do that.
0:16
In the activity bar, I'm going to click on the Run and Debug section. In there, I'll click on the link that says, Create a launch.JSON file.
0:24
Now, this is going to pop up a window that'll show us some options that make sense for Python developers because we're building a Python app.
0:31
I'm going to select the option for module, and now I'm going to type in the name of the module of our application.
0:37
In this case, it just happens to be Podcast API. Now, Visual Studio Code is going to create a launch.JSON file for us. As you can see, this contains
0:46
a single configuration that allows us to run and debug our module. Now, one small change I'm going to make here is I'm just going to rename this,
0:54
and I'm just going to call this Podcast API. If we take a look at some of the other settings, you'll notice that the type is set to Python.
1:01
You'll see the request is set to launch, which is important because this tells VS Code that we wanted to
1:06
launch our application and then attach the debugger to it, as opposed to attaching our debugger to an already existing process,
1:13
which is a completely different setting. We have our module, and then we have some other settings down there as well.
1:19
Another thing I want you to pay attention to, if we head back over to the Explorer, and we take a look inside of that.VSCode folder,
1:26
you'll see that VS Code is now created that launch.JSON file. Now, let's go ahead and run this debugging session and see what happens.
1:33
Now, inside of this run and debug section, I'm going to open this up a little bit so we could see it a little bit better.
1:38
The drop-down at the top is going to show us a listing of all our different configurations. Right now, we have this one that says Podcast API module.
1:47
If you'll notice, that matches the name of the configuration we have in our launch.JSON file.
1:52
I'm going to select that one and go ahead and click ""Run"". As expected, Visual Studio Code is going to activate our version,
1:59
and it's going to run the debugger. Now, if you go to the virtual environment, it's going to start the debugger,
2:04
and it's going to attach to our application, and you should see those familiar log messages right there in the terminal.
2:10
Why don't we go ahead and create a different type of launch configuration? I'm going to stop the debugger.
2:15
I'm going to move this over a little bit so we can have some more space, and let's close the terminal for now.
2:21
Instead of going through what we did before and clicking on that link, right here inside of the launch.JSON file,
2:29
we're going to go ahead and create a new one. This time, now it's going to show us a listing of
2:33
a lot of the different configurations that it has by default. As you can see, Python is right at the top, which makes sense for us,
2:39
but there's also options for Chrome, Edge, Node.js. If you have other language support in there, you might see them for Java, .NET, etc.
2:48
Let's go ahead and click on the Python one, and this time I'm going to select FastAPI, because our sample application is actually built with FastAPI.
2:55
I'm going to hit ""Enter"". Now, it's asking us for the paths for our application. If I take a look inside of our Explorer,
3:02
you can see that it's PodcastAPI, and the main app actually exists inside of here.
3:08
But what this actually wants is the name to the FastAPI module that we want to run. This is what we're going to do.
3:14
I'm just going to type in PodcastAPI, and I'm going to put a .API. Notice as it creates this new configuration,
3:23
it has a colon on the right-hand side with a value of app. Now, we actually need to change this, and let me show you.
3:29
Let's say I want to call this API. If I open up our API.py file,
3:33
you'll notice that the name of our application that's getting exported is actually API, and it's not app. And I did this on purpose.
3:40
So what we need to do is just rename this really quickly, and call this API, or whatever the name of your particular module happens to be.
3:48
Now, I'm going to do what I did the last time, and I'm going to rename this really quickly, call this PodcastAPI.
3:54
But this time, notice the postfix is different. This time, this one says PodcastAPI FastAPI.
3:59
Just so that we know that the names for these different configurations are different. But the type is the same. It still says Python.
4:06
The request is the same. It still says launch. But what's different now is that you can see that it's using Yubicorn, which is a server,
4:13
and then it's passing it some command line options using that args key. So it's passing it the name of the module, which is PodcastAPI.API,
4:23
and it's also passing it the reload flag as well. Let's head back over to the debug section. Open it up.
4:31
Now, I should be able to select from one or the other of these debug configurations. Now, this FastAPI one is the one that we created last.
4:38
Let's go ahead and run it. And now you see it's running our application.
4:43
Now, what are some of the things that you notice might be a little bit different?
4:47
Well, first of all, when we run it like this using this FastAPI option, what we need to do is we need to give it a FastAPI object.
4:56
Let me move this window out of the way so you can see what I'm talking about. So we need to provide it this object,
5:01
and it's going to pass it over to the server. This is going to start running our application from here, which is fine.
5:06
With the other option I did when I read it as a module, it's actually going to end up running the code that's in main.py,
5:13
which is a little bit different. And as you can see, my main function does a couple more things.
5:17
It gets a parser, and it looks at some of the command line args. It generates fake data for me, and then it actually calls uvcon for me.
5:25
So again, I just want to show you this because you might have different ways to run your application.
5:30
And now you can kind of see some different ways where you could get more flexibility
5:34
and also rely on some of the built-in functionality that's inside of VS Code. Let's head back over to the launch.json file for a second.
5:41
Now, if I come down here, and let's say I go down to the next line, and I hit Control + Space, and this should work on any operating system, right?
5:49
We're going to hit Control + Space. Now, notice this is going to show us some of the other options that are available for us
5:54
as we're setting up this debug configuration. So some examples. Examples are .env for adding environment variables,
6:01
or .env file to point to the environment variable file that's in your workspace. I also have options for Django.
6:08
I have options specific to Linux or Mac if you wanted to. You have path mappings and programs, purposes, all these different options that you can set.
6:16
And like I said, we're not going to go through all of these, but I just wanted to show you some of the different options that are available for you.
6:22
And if you hit Control + Space, IntelliSense will kick in and allow you to explore some of the options
6:28
that you might want to set for your debug configurations as well. Now, as you can see, with our debugger still running,
6:36
we can run our application just like we did before. So let's say I head back over to the categories router.
6:42
I'm going to go ahead and add this breakpoint here. And now if we open up the sample.http, I can go ahead and send requests.
6:50
And notice, now I can start stepping through my application. I can hit Step Over, I can hit Continue. I can get the results.
6:58
I can get the results as I expected to. And again, what I'm doing now is instead of just running like an ad hoc debugging session,
7:04
I'm actually running it based on some of the information that I have set up inside of this file.
7:09
Now, if you're not using PaaS API, you're using some other framework,
7:12
or maybe you just want to run a module, or maybe you just want to debug a Python file,
7:16
I definitely recommend you check out some of these different configurations that are available.
7:20
And I definitely recommend you head over to the documentation so you can learn about all of the different settings you could supply,
7:26
all the different options that are available for you.
7:28
So you can make sure that you can create these debug configuration files that make sense for your project.
7:33
But the great thing about this is that now I have a configuration that's set. So I don't have to worry about recreating this every time.
7:40
And I can even check this into source control so I could share with my team, or I could have it available for me whenever I switch machines.