You can use ʻimport pdb`, but here we will show you how to debug without changing the source code. Only frequently used commands will be covered.
$ python -m pdb XXXXX.py
Or
$ python3 -m pdb XXXXX.py
hello.py
msg = "Hello world"
def hello(txt):
print(txt)
hello(msg)
print("Done.")
--pdb execution example
$ python3 -m pdb hello.py
> /path/to/current/directory/hello.py(1)<module>()
-> msg = "Hello world"
(Pdb)
You can proceed with debugging by entering a command after (Pdb)
.
--Frequently used commands
command | function |
---|---|
b(reak) [Number of lines or function name] | Set a breakpoint on a row or function |
c(ont(inue)) | Run until the next breakpoint is reached |
s(tep) | Run current line(Stop at a function call) |
n(ext) | Run current line(If it is a function call, execute the function) |
q(uit) | Exit the debugger |
You can omit the characters in parentheses in the command.
$ python3 -m pdb hello.py
> /path/to/current/directory/hello.py(1)<module>()
-> msg = "Hello world"
(Pdb) b 6
Breakpoint 1 at /path/to/current/directory/hello.py:6
(Pdb) c
> /path/to/current/directory/hello.py(6)<module>()
-> hello(msg)
(Pdb) s
--Call--
> /path/to/current/directory/hello.py(3)hello()
-> def hello(txt):
(Pdb) s
> /path/to/current/directory/hello.py(4)hello()
-> print(txt)
(Pdb) n
Hello world
--Return--
> /path/to/current/directory/hello.py(4)hello()->None
-> print(txt)
(Pdb) c
Done.
The program finished and will be restarted
> /path/to/current/directory/hello.py(1)<module>()
-> msg = "Hello world"
(Pdb) q
$
http://docs.python.jp/3/library/pdb.html
Recommended Posts