DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Python Console Debugger


Create a python file and debug it in gdb style with pdb:

$ pdb testpdb.py
> /home/chad/docs/tmp/testpdb.py(1)<module>()
-> aa = 3
(Pdb) help
...
(Pdb) help l
...
(Pdb) l
  1  -> aa = 3
  2     bb = 4
  3     cc = aa + bb
  4     print(cc)
[EOF]
(Pdb) w
  /usr/lib/python2.7/bdb.py(400)run()
-> exec cmd in globals, locals
  <string>(1)<module>()
> /home/chad/docs/tmp/testpdb.py(1)<module>()
-> aa = 3
(Pdb) n
> /home/chad/docs/tmp/testpdb.py(2)<module>()
-> bb = 4
(Pdb) b 4
Breakpoint 1 at /home/chad/docs/tmp/testpdb.py:4
(Pdb) c
> /home/chad/docs/tmp/testpdb.py(4)<module>()
-> print(cc)
(Pdb) w
  /usr/lib/python2.7/bdb.py(400)run()
-> exec cmd in globals, locals
  <string>(1)<module>()
> /home/chad/docs/tmp/testpdb.py(4)<module>()
-> print(cc)
(Pdb) p bb
4
(Pdb) c
7
The program finished and will be restarted
> /home/chad/docs/tmp/testpdb.py(1)<module>()
-> aa = 3
(Pdb) q

The command is exactly the same with gdb.

On some OS like ArchLinux, there isn't "pdb" command, so you have to find the location of "pdb.py", then define an alias in ~/.zshrc:

$ locate pdb.py
...
/usr/lib/python3.4/pdb.py
...
$ echo 'alias pdb="/usr/lib/python3.4/pdb.py"' >> ~/.zshrc

Now you can use "pdb " as in Mint.



Published

May 28, 2014

Last Updated

May 28, 2014

Category

Tech

Tags

  • console 9
  • debugger 2
  • python 136

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor