16

I like testing functions in the Python interpreter. Is it possible to debug a function in the Python interpreter when I want to see more than a return value and a side effect?

If so, could you show basic debugger operations (launching the function with arguments, setting breakpoint, next step, step into, watching variable)? If not, how would you debug a function another way?

The point is, I want to debug only a particular function which will be supplied with arguments. I don't want to debug whole module code.

thank you for advice

xralf
  • 3,312
  • 45
  • 129
  • 200
  • 1
    Have you searched for `pdb` with Google? – hochl Apr 24 '12 at 10:25
  • @hochl I thought that pdb is an application (something like gdb for C language) and I didn't know that it's a module. I will try it, if it's able to debug particular function. – xralf Apr 24 '12 at 10:32
  • It is -- `pdb` is the way to go, read the [module description](http://docs.python.org/library/pdb.html). – hochl Apr 24 '12 at 10:33

3 Answers3

12

If you want to debug specific function you can using this -

>>> import pdb
>>> import yourmodule
>>> pdb.run('yourmodule.foo()')

over the command line. pdb.set_trace() should be added in your function to break there.

More info on pdb can be seen here - http://docs.python.org/library/pdb.html

hochl
  • 12,524
  • 10
  • 53
  • 87
Karthik Ananth
  • 211
  • 1
  • 8
  • 1
    Can I find out the line numbers where should I set a break point in the interpreter or should I find out it from the editor? – xralf Apr 24 '12 at 10:34
  • OK, now I understand. I should place `pdb.set_trace()` in my source code. Nice job, thanks. I haven't understood it before. – xralf Apr 24 '12 at 13:34
7

See pdb module. Insert into code:

import pdb
pdb.set_trace()

... makes a breakpoint.

Bittrance
  • 2,202
  • 2
  • 20
  • 29
5

The code-to-debug does not need to be modified to include pdb.set_trace(). That call can be made directly in the interpreter just before the code-to-debug:

>>> import pdb
>>> pdb.set_trace(); <code-to-debug>

For example, given test_script.py with the following code:

def some_func(text):
    print 'Given text is {}'.format(repr(text))
    for index,char in enumerate(text):
        print ' '*index, char

an interpreter session to debug some_func using the debugger commands step-into (s), next (n) and continue (c) would look like:

>>> import pdb
>>> import test_script
>>> pdb.set_trace(); test_script.some_func('hello')
--Call--
> c:\src\test_script.py(1)some_func()
-> def some_func(text):
(Pdb) s
> c:\src\test_script.py(2)some_func()
-> print 'Given text is {}'.format(repr(text))
(Pdb) n
Given text is 'hello'
> c:\src\test_script.py(3)some_func()
-> for index,char in enumerate(text):
(Pdb) c
 h
  e
   l
    l
     o
>>> 

See the docs for the pdb module for more information on how to use the debugger: http://docs.python.org/library/pdb.html

Additionally, while using the debugger, the help command provides a nice list of commands and help <command> gives help specific to the given command.

gunderbolt
  • 163
  • 2
  • 9
  • Aha nice, the code I need to step through requires a lot of setup, so the accepted answer ends up being a huge pain - this works very nicely though. – Izkata Apr 27 '21 at 20:44