You are on page 1of 4

PROFESSOR: Welcome back.

So in the last video, we've walked


through the process of creating a new project in PyCharm
from scratch, and we also walked through the process
of creating configurations for each script we're
trying to run.
And we also talked about how to run
our script with or without a conda environment.
OK, so in this video, we are going
to go over one of the main functions
or main features of using any IDE, including PyCharm,
for developing projects.
That's this debugging capability, OK?
So now, in this main script here, we
tried to add more lines.
So first of all, we tried to assign some values
to certain variables.
OK, so m equals to 3, m equal to 4.
We also tried to write some function in this utility
script, OK?
So let's say we try to define a function name that's
called to_square, which essentially, you
just multiply a variable to yourself and make it squared,
OK?
And then we return this value.
And then we try to import this function.
We just define into the main script, OK?
The way to do that is, from the script, import star star
means all the functions defined in this script.
OK, so you can see, maybe you defined
a lot of utility functions in this utility script,
and then you can import all of them
into the main script like this, OK?
So here, we can do something like this.
We can have a result, 1 equals to_square.
So here, you can see PyCharm actually
knows we tried to import this function into the main script,
so it gives us this choice.
So we just type enter to automatically complete
this function.
Also notice, after we're using this function,
this line now lights up, OK?
But the previous line, import pandas, is still gray,
so that means this line, we haven't really used it, OK?
So even we import pandas, but we have not
used any of the functions or capabilities within pandas, OK?
The same thing before--
if we don't have this line, then this line
will become gray again, OK?
So that means we didn't really use this line, OK?
So by now, if we come back again,
now, this line lights up, OK?
So let's add another line here, say, result 2 equals to_square
m, OK?
And then we try to print these results.

OK?
So let's first try to just run this script
to see when it could get corrected results.
All right, and then we get this, which
are correct results, right?
So the n to the square is 9 and m to the square is 16, OK?
And so now, we try to use the debug function of PyCharm.
So the way to do that is first, you place something
called breakpoints in this gray area.
The way to place any breakpoint is just
to click each line like this, OK?
And the way to cancel our breakpoint
is just to click itself again, OK?
So now, let's try to put a breakpoint here.
And now, if we click this bug icon--
it says Debug Main--
we enter the debugging mode, OK?
What the debugging mode is just doing
is it's executing this program until this line, OK?
So it's not going to execute this line,
but it's running the program until the previous line.
OK, so here, we can see the PyCharm actually
give us this, the value of this variable,
n, where it tells us this n equals to 3, OK?
And also, PyCharm has this interesting feature
which shows up here.
It's this little calculator icon.
So it just says evaluate expression.
So what does it do is you can actually
type some simple expressions involving the variables already
have assigned values.
And you can just try to test the values of those expressions.
So for example, here, I can try to test,
what if I try to add a 1 to this n variable?
OK, so it tells me this equals to 4, OK?
So it knows n equals to 3, and now it can give me
the correct answer, OK?
And also, all these variables values
are showed up here in this variable section, OK?
OK, so let's say we want to move on to--
in this script, we try to execute another line, which
is m equals to 4.
The way to do that is to click here, is Step Over, OK?
So Step Over means you'll try to run another line
within the same script, OK?
So we can make a step over.
So now, you can see, PyCharm tells us m equals to 4,
and also, it shows up here as well.

OK?
And now, let's say we have already finished our debugging.
We just try to run the whole program through.
The way to do that is to click here,
which says Resume Program, OK?
So if we do this and we go to the Console,
and it will give us all the correct results.
OK, basically, you just resume running this program.
OK, so let's try this Debug function again.
OK, now, n equals to 3, m equals to 4, which
we go over it, which is good.
Now, we have another function here which is called to_square.
Now, maybe this function will give us some error, OK?
And if we try to go into that function,
then we can no longer do that by using Step Over.
We have to use something called Step Into, OK?
So now, if we click Step Into, it will actually
direct us to this file, OK?
To where this function is defined, OK?
And the way to jump out from this file
is to click the Step Out.
Now, we went back to here, OK?
And then you will notice, here, you have actually two icons.
One is called Step Into and one is called Step Into My Code.
So the difference is, Step Into allows
you to go to any function, not only defined in your own code
but also defined in some existing software packages, OK?
So for example, if this to_square
is a function defined in pandas, Step Into
will allow us to go to where pandas define this function,
OK?
And the Step Into My Code will not do that
but will only go into the function
you wrote in this project, say this utility script, OK?

So in practice, you're probably going to use Step Into My Code


more often, because usually, you will not modify existing
libraries that much, OK?
OK, sorry, you may find there's a bug in an existing library
and then you can fix it, and I don't know, maybe
that will make you all famous or a contributor
to some famous existing packages,
but usually, you don't need to use that.
We will treat existing packages most
of the time as correct, OK?
So that's the difference between Step Over,
Step Into, Step Into My Code, and Step Out.
And also, resume running the whole program through.

OK, that's basically very basic functions related to debugging.


Also, if you want to exit this debugging mode,
you can just stop me, OK?
So this just stops the whole thing
so you can either run this script again or debug
this script again.
All right, so that's basically everything
we wanted to cover in these two videos.
In the first video, we talked about the PyCharm interface.
We talked about how to create a new project in PyCharm
and how to run such a project, with or without a conda
environment.
And in this second video, we mainly
talked about how to debug in PyCharm.
Obviously, these two videos are not comprehensive tutorials
about PyCharm at all.
The goal here is to introduce you to PyCharm
and to motivate you to use PyCharm for your projects.
PyCharm is very powerful and has many, many features,
and hopefully, after watching these two videos,
you feel comfortable to play with PyCharm.
And hopefully, you will find PyCharm is very useful for you.
All right, I'll see you in the next video.

You might also like