[파이썬] pdb `list` 또는 `l` 명령어로 소스코드 확인
To use the list
or l
command in pdb
, follow the steps below:
- Start by importing the
pdb
module at the beginning of your Python script:
import pdb
- Next, set a breakpoint in your code where you want to start debugging. This can be done by adding the following line:
pdb.set_trace()
-
Run your program and wait for it to reach the breakpoint. Once the breakpoint is hit, you will see a
(Pdb)
prompt in your console. -
To view the source code, you can use either the
list
orl
command:
- To display the current line and 5 lines above and below it, simply type
list
orl
and press Enter:
(Pdb) list
- If you want to view a specific range of lines, you can provide the line numbers as arguments. For example, to display lines 10 to 20, type:
(Pdb) list 10, 20
- If you only want to view a specific line, provide its line number as an argument. For instance:
(Pdb) list 15
- The source code will be displayed in your console, and the line currently being executed will be marked with an arrow (
->
). You can navigate through the code by using thel
command with different arguments or pressing Enter to repeat the lastlist
command.
Using the pdb
list
or l
command can greatly assist you in understanding the flow of your program and identifying any errors or unexpected behavior. It allows you to examine the source code while interacting with the program during debugging sessions.
Remember to remove or comment out the pdb.set_trace()
line once you have finished debugging, as it will pause the program execution every time it is reached.