Reputation: 163
How can i debug my python code step by step if i want to by placing the red dots
from terminal as i used to do that on PyCharm
but having difficulties with the debug when i run my file from the python terminal using the command
python testings.py --annotations=input.csv
Upvotes: 1
Views: 10558
Reputation: 478
You can use Pdb for debugging using the command line.
It has a argument b(reak) that allows you to specify a line number where you want to have a break point.
It's definitely more cumbersome than using a proper IDE, but certainly helps in certain situations.
Example:
Simple example script (example.py):
a = 2
b = 5
c = a + b
print("{0} + {1} = {2}".format(a, b, c))
Starting debugger:
python3 -m pdb example.py
Now the debugger will start and will point to the first line. We can then use the commands (see documentation linked above) to step through the code. An example:
> /home/[...]/example.py(1)<module>()
-> a = 2
(Pdb) b 4
Breakpoint 1 at /home/[...]/example.py:4
(Pdb) c
> /home/[...]/example.py(4)<module>()
-> c = a + b
(Pdb) s
> /home/[...]/example.py(6)<module>()
-> print("{0} + {1} = {2}".format(a, b, c))
(Pdb) p c
7
(Pdb)
Explanation: Every line with (Pdb)
is a prompt where you can enter commands. Here are the commands I entered:
b 4
sets a breakpoint at line 4. This is confirmed by the next line.c
continues running the script until the next breakpoint is hit. As you can see in the next line, the debugger now stops at c = a + b
which is line 4 of the script.s
performs one step, so we are now at the print statement.p c
prints the value of expression "c" which in this case is simply the value of the variable c
: 7For more commands and their explanations, again, take look at the official documentation. Hope this serves as a very basic introduction on how to use Pdb.
Upvotes: 2