python debugger

Started by
8 comments, last by Emmanuel77 15 years, 10 months ago
How do I debug in python? ( I've done a little debugging in c++ using the IDE Code::Blocks. ) (1) Do you have a tutorial to get me started. (2) Is there a GUI that makes it easier, probably in a python IDE?
Advertisement
I use SPE (Stani's Python Editor) which includes a debugger built in. The debugger isn't anything compared to ones in, say, Microsoft Visual Studios, but it works fine, is free, and is fairly easy to pick up if you know debugging basics (breakpoints, stepping through code). My only issue with it is that I can't add breakpoints inside the code editor, just inside the debugger. That makes it slightly annoying if the breakpoint needs to be in a separate file. SPE is fairly nice for editing, too.
Have you played around with the built-in debugger? It's pretty easy and it gets the job done.

All you have to do is "import pdb", and then add a call to "pdb.set_trace()". When it reaches this call, it will stop and give you an interactive prompt, where you can do all sorts of things. You can look at the nearby source code ("l"), look at the call stack ("w"), step through ("s" or "n"), or type in random statements. There are other commands too, you can type "help" to see them.

In my code I have a version of assert that I sprinkle everywhere, and it just calls "pdb.set_trace()" if the condition is false. It saves a ton of time because I don't need to go play detective, I can immediately find out why something went wrong.

pdb.pm() is another great command. The full docs for PDB are here: http://docs.python.org/lib/module-pdb.html

It's not a GUI though.
Quote:Original post by pinacolada
In my code I have a version of assert that I sprinkle everywhere, and it just calls "pdb.set_trace()" if the condition is false. It saves a ton of time because I don't need to go play detective, I can immediately find out why something went wrong.


That is a fantastic idea. Are you doing something like this?

def _assert(exp, msg=""):	if __debug__ and not exp:		import pdb		print "_assert failed:", msg		pdb.set_trace()
Seeing as I use Komodo IDE, I get a really nice graphical debugger with all the features you'd expect after using Visual Studio for over a decade: set breakpoints in the editor, step in, step over, step out, locals window, watch window, ability to modify object values, call graph, ability to interrupt execution and drop into an interactive session with the running code, I/O redirection to the output window, debug parameters (set working directory and command line arguments)... The only thing that is iffy is run to cursor, and the only thing that's missing is the ability to drag the next statement instruction pointer.

Unfortunately, Komodo IDE only comes in Professional variant now (I own the last version that was available with a Personal license, for $35). I'll upgrade eventually, but seeing as it's $300, I'm in no hurry (thought 4.x introduces sweet source code control integration, among other enhancements).

Hmm. There's now a Student License, though.
Quote:Original post by doctorsixstring

That is a fantastic idea. Are you doing something like this?

*** Source Snippet Removed ***


Yup, pretty much like that. When I release my app to the public, I'll change _assert to just throw an exception, since I figure that the typical user would be a little surprised to see a debug prompt.
Next time I start messing with Python I think I'll try using this for debugging since I'm used to the Visual Studio debugger:

IronPython Studio is a free full IDE (Integrated Development Environment) for the Python programming language. It is based on the existing IronPython example that is included in the VS SDK.
IronPython Studio is based on the Visual Studio 2008 Shell runtime (royalty free) and can be installed without requiring any version of Visual Studio. It is available for Visual Studio 2008 Shell Isolated and Integrated modes.

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Quote:Original post by daviangel
Next time I start messing with Python I think I'll try using this for debugging since I'm used to the Visual Studio debugger...

Just remember to keep in mind the differences between IronPython and CPython.
I recently ran across a pretty nice debugger gui for python called winpdb. It is really easy to use, completely FREE and works pretty well. You can step through code, set break points, and it shows global and local variables and can do all sorts of other things that I haven't had time to figure out yet. It solved my problem of no debugger nicely. The best part is its free and cross-platform. I use it on my mac and all I do is "winpdb myscript.py" and it opens up the gui and works quite nicely.

Wingtoad
Wingtoad
Another great advantage with winpdb is that you can use it to debug embedded python.

as far as I know, it is the only free debugger to do that ( with HAP, but Hap is not maintained anymore )

Emmanuel

This topic is closed to new replies.

Advertisement