How to debug python code?

S

sushant.sirsikar

hi,
I am new to Python programming.I am not getting exactly pdb.Can
anyone tell me effective way to debug python code?
Please give me any example.
Looking for responce.
Thank You.
Sushant
 
D

Dennis Lee Bieber

hi,
I am new to Python programming.I am not getting exactly pdb.Can
anyone tell me effective way to debug python code?

I think I toyed with pdb back around 1993... Never needed it...

Of course, with so many different languages and debuggers in my
life, I've never found time to master any but the old VMS debugger
(which is nothing more than a very complex error handler <G>)

My typical debugging technique is the infamous "wolf fence" (there's
one wolf in Alaska, how do you find it? First build a fence down the
middle of the state, wait for the wolf to howl, determine which side of
the fence it is on. Repeat process on that side only, until you get to
the point where you can see the wolf). In other words; put in a few
"print" statements until you find the statement that is failing (then
maybe work backwords from the "tracks" to find out where the wolf/bug
comes from).
--
 
B

bruno at modulix

hi,
I am new to Python programming.I am not getting exactly pdb.Can
anyone tell me effective way to debug python code?

(automated) unit tests + print statements + the interactive shell are
usually enough. I almost never used pdb in 5+ years of Python programming.
 
R

Ravi Teja

1.) Print statements
2.) IDEs

Most Python IDEs provide visual debuggers so that you don't have to use
command line ones such as pdb.

As with all languages that allow to be executated as a script as well,
print statements usually get the job done quite well in most cases.

Please read the Python FAQ (as you should for any new language you
learn) in its entirety.
http://www.python.org/doc/faq/
It answers your IDE question you posted in the other thread as well as
many of your future questions quite well.
 
T

Thomas Guettler

Am Thu, 30 Mar 2006 21:18:50 -0800 schrieb sushant.sirsikar:
hi,
I am new to Python programming.I am not getting exactly pdb.Can
anyone tell me effective way to debug python code?

Hi,

I try to debug the code while I type: Use "assert".

Then if you get an AssertionError you can insert print statements or
raise("var=%s var2=%s" % (var, var2))
to narrow down the problem. I never used pdb.

HTH,
Thomas
 
G

Grant Edwards

I am new to Python programming.I am not getting exactly pdb.Can
anyone tell me effective way to debug python code?

1) Read your code. Think.

2) Add some "print" statements.

3) goto 1)
 
R

R. Bernstein

hi,
I am new to Python programming.I am not getting exactly pdb.Can
anyone tell me effective way to debug python code?
Please give me any example.
Looking for responce.
Thank You.
Sushant

Well, I guess (in addition to the other good suggestions in this
thread) this is an obvious place to plug pydb
http://bashdb.sourceforge.net/pydb

If you are using pdb, I think you'll find pydb, um, better.
 
R

R. Bernstein

Dennis Lee Bieber said:
I think I toyed with pdb back around 1993... Never needed it...

Of course, with so many different languages and debuggers in my
life, I've never found time to master any but the old VMS debugger
(which is nothing more than a very complex error handler <G>)

That's one reason why in my bash and GNU make debugger (and in
extending pdb), I've stuck to the gdb command set: the effort that is
spent mastering gdb can be transfered in the GNU Make, Bash, *AND*
Python debuggers.
 
P

pruebauno

hi,
I am new to Python programming.I am not getting exactly pdb.Can
anyone tell me effective way to debug python code?
Please give me any example.
Looking for responce.
Thank You.
Sushant

If you are having issues you also might want to try:
http://pychecker.sourceforge.net/
and see if it finds your problem. You might get lucky.

Or just make the bigger install:
http://stani.be/python/spe/blog/
that one already includes pychecker and a python debugger.

Sorry for the noise if you were just looking for instructions for pdb.
 
J

Johannes Nix

Dennis Lee Bieber said:
My typical debugging technique is the infamous "wolf fence" (there's
one wolf in Alaska, how do you find it? First build a fence down the
middle of the state, wait for the wolf to howl, determine which side of
the fence it is on. Repeat process on that side only, until you get to
the point where you can see the wolf). In other words; put in a few
"print" statements until you find the statement that is failing (then
maybe work backwords from the "tracks" to find out where the wolf/bug
comes from).


I think the advantage of this method is that one has to become
clear what the state of the program should look like.

For the same reason, unit tests may help to develop a
proper specification of the task of a module.

However, pdb's port mortem debugging can be helpful when a
program takes a lot of time to hit the error, and
a large amount of data is in play, for example for
numerical computations.

Johannes
 
J

james.wondrasek

I don't have an example, but what I do is insert:

import pdb
pdb.set_trace()

before the code that I think is causing the problem
(or that the traceback barfed at).

Then hit 'l' (lowercase L) to get a list of the code
which is being executed.

Use 'p' to look at variables. eg p some_local

Use 'n' to execute the current line of code
(doesn't follow method or function calls)

Use 's' to step through code, including following
function and method calls.

Finally, 'c' to continue execution as normal.

There are other options, but that should get you started.
And look at the article linked elsewhere in this thread.

pdb - because fencing off Alaska is too damn slow, binary
search or not.

jtm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top