|
| 1 | +# Debugging in pycharm |
| 2 | + |
| 3 | +To prepare PyCharm for debugging, you need to specify a so-called breakpoint. This is the line after which the debugger |
| 4 | +will start and the code will step through. Since our program is small, we will tell the debugger to start from the |
| 5 | +beginning. To put a breakpoint, you need to click the mouse to the left of the needed line and to the right of its |
| 6 | +number. A red circle will appear at this point, indicating that a pause will occur at this point. To remove the |
| 7 | +breakpoint, you need to click on the red circle again. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +Then you have to launch the debugger, in order to launch it you can either click on the debugger icon in up right corner |
| 12 | +or press *shift + f9* if you use windows or linux and *option + D* on mac. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +We now see two tabs: *Debugger* and *Console*. In the console, we see everything that the program has displayed so far. |
| 17 | +Since we stopped execution at the very beginning, the console will be empty for now. It is the debugger window that is |
| 18 | +of interest to us, since here we can track the change of all variables. As you can see, we already have a list of |
| 19 | +Special Variables, in which you can see the values of such utility variables as `__name__`. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +To start tracking a new variable, enter the name of the variable to the input field and click on a plus icon. First, we |
| 24 | +will see the inscription name 'i' is not defined, which means that our variable has not even been created yet. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +Let's then tell the debugger to process the next line of code. It is important to note here that there are 2 main |
| 30 | +ways of stepping: *step into* and *step over*. |
| 31 | + |
| 32 | +### step into |
| 33 | + |
| 34 | +When stepping into, the debugger will "enter" inside all functions and other constructs if it encounters them. So, if |
| 35 | +the debugger comes to calling some function func(), then it will go to the part of the code where its contents are |
| 36 | +described. To make one step into, press F7. |
| 37 | + |
| 38 | +### step over |
| 39 | + |
| 40 | +With the step over, the debugger will not “enter” all functions and other constructs, but will simply execute the |
| 41 | +entire code of the function without stopping and continue execution further. To make one step over, press F8. |
| 42 | + |
| 43 | +### Process itself |
| 44 | + |
| 45 | +In our example, it doesn't matter what to use, since we don't have any functions. So let's use the over step. |
| 46 | +When stepping through each line, we will go through the loop 9 times and see how the variable i changes 9 |
| 47 | +times: |
| 48 | +<br/> |
| 49 | + |
| 50 | +*press F8* |
| 51 | + |
| 52 | + |
| 53 | +*press F8*, twice(because we go to the loop line itself firstly) |
| 54 | + |
| 55 | + |
| 56 | +*press F8*, twice(because we go to the loop line itself firstly) |
| 57 | + |
| 58 | + |
| 59 | +You can either stop debugging manually or wait until the debugger has processed all the lines in the program, which is |
| 60 | +almost never found in practice. You can also create multiple breakpoints. |
| 61 | + |
| 62 | +### Conclusion |
| 63 | + |
| 64 | +All in all, debugging is an indispensable thing in the development of real projects, because it is the most effective |
| 65 | +tool for finding errors. |
0 commit comments