Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging code in the Python interpreter

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

like image 349
xralf Avatar asked Apr 24 '12 10:04

xralf


People also ask

How do you debug your Python code?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

What is debugging in Python with example?

A debugger is a program that can help you find out what is going on in a computer program. You can stop the execution at any prescribed line number, print out variables, continue execution, stop again, execute statements one by one, and repeat such actions until you have tracked down abnormal behavior and found bugs.

What is a debugging tool in Python?

The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame.


1 Answers

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

like image 146
Karthik Ananth Avatar answered Sep 29 '22 03:09

Karthik Ananth