Hi, my name is Luciana, I'm a program manager
for Python in Visual Studio code and in this video
I'm going to be showing you three Python debugger
features
that will help you find and fix bugs faster in
Visual Studio Code.
First things first, make sure you have Visual Studio
Code installed as well as the Python extension.
If you don't, you can just follow the links to
do so.
Then you will want to get started with the debugger to
configure it.
So once you open a Python file,
it will activate the Python extension and then you
can open this debug view by clicking on this icon
in VS Code and then you can click on create
an launch.json file. A launch.json file is just a
configuration file to define what are the arguments for your
debugger an what kind of applications you will debug. Once you
click on it you can select Python and then you
will have another list of selections you can make depending
on what kind of application you're working with.
If you're doing web development
with Django, Flask, FastAPI or even Pyramid,
you can select these options,
but if you're only looking to debug a simple script
you can select the first option that is just a
Python file.
There are also other configurations for remote attach as well
as attaching to processes running on your machine as well
as Python modules.
So you really just want to select the
option that applies to your project that you're working on.
The first feature I'm going to be covering is the
debug console.
I'm going to open Visual Studio Code here.
I have an error that I have a path defined
that my code is not finding it,
so I'm going to show you how we can use
the debug console to figure out how to fix this
problem.
So I'm going to get this line here.
mask_path.parent 'cause that's what I'm trying to see...
to find the file at and I can see that
it's giving me a file which
is basically non existing. And if I scroll down to
where this call started I can see that it was
here and I can try to test a fix for it
in the debug console. So for example if I do...
if I call this line to get the parent of
the file that I'm in... the folder parent,
it will tell me that it's pvscc-logo.
So that is basically the workspace root and it's saying
that I'm trying to access images but images is not
under that folder, so I can see if a
fix that would work would be to add
"logo_app"
and "static" to my path. And once I do so,
I can see that this is a path that is
actually existent in my application.
All thanks to the debug console.
So great, I can go ahead and copy this value
and fix it in my code.
If you're interested in seeing if your fix has actually
worked,
you can come ahead and select a piece of code.
Here we will redefine what mask_path is by right
clicking and selecting
"Evaluate in debug console" and this will simply copy and paste
the code to the debug console.
If I do mask_path,
I can see that it actually defined this new value
and if I scroll up again,
I'm able to do the same thing.
Select the code, right click,
And then run evaluate debug console as well to verify
my fix.
The next debugger feature I'll be covering
are conditional breakpoints. If you ever wanted to understand the
behavior of a code only when a certain condition is met
this is the feature for you. Sometimes
Yeah, you can just add ifs and elses to your code
and print statements,
but it's much easier when you don't need to edit
your code to understand what's going on.
So I'm going to show how you can do that
in VS code.
I'm opening VS Code again and here I can see
that I have a path and everything like we were
doing,
but I would like to understand the behavior of this
code once
this image that I'm trying to generate already exists and
I can keep deleting or not deleting my code and image and
all of that,
but I could just right click on this breakpoint
and click on edit breakpoint and then I can insert
a condition that is met.
So if you're working with variables you could do
"if i == 5"
or anything else. In my case here,
I want to make sure this will stop only when
this condition is met and if the condition is never
met,
it will simply skip that breakpoint and the execution will
continue ignoring that.
But if the condition is met,
like in this case, the execution will stop at that
point.
As we can see here.
The third feature I'll be convering today is jump to cursor.
This is my favorite debugger feature of all times.
It can save a lot of time for me.
I'm going to open VS Code and show you how
it works.
So in here I have my execution and as I'm
trying to execute this line 16 here,
you're going to see that it's going to take awhile
because I'm trying to generate a word cloud based on
a few words in the documentation and then I'm going
to generate an image and that will not take as
long.
But the thing is, sometimes I will want to re
execute previous lines of code that my debugger has already
executed, to understand the behavior of my code before I
reached that point. Typically what one would do is to
restart the debugger so the code could be executed again.
The problem is that you saw how long it took
to generate my word cloud.
I don't want to keep waiting for that every time
I want to evaluate pieces of code that the debugger
has already passed by, just because I forgot to set
a breakpoint earlier on. The good news is that you
don't have to.
If you right click on the line that you want
to execute next,
you can go to jump to cursor and that will
execute...
That will define what is the next line you want
to execute and I could go as far back as
the first line of my block and then I can
execute that one, and if I want to ignore the
regeneration of my word cloud,
I can jump to the other line and then click
on jump to cursor.
This really defines what is going to be the next
line the debugger is going to execute,
and we have this fined
control of what's going to be executed next,
which can save a lot of time and be super
super useful.
So these are the three features I wanted to show
you today.
The Pytohn debugger is very powerful.
There's so much more it can do,
and I'm certainly you'll find
even more features you'll fall in love with,
So make sure you try it out.
Let us know what you think.
You can follow us on Twitter.
You can also connect with us in our Python Discord
server through the following links.
Thank you so much.