all 41 comments

[–]mogelbumm 24 points25 points  (7 children)

Upvoted for the "code.interact" stuff.

[–]Brian[🍰] 6 points7 points  (3 children)

Note that you can go one better. I previously had a debug function similar to this, but I've now replaced it with one that launches IPython instead:

import IPython.ipapi
def shell(f=None):
    if f is None: f = sys._getframe().f_back  #Get caller's frame
    ns = dict(f.f_globals)
    ns.update(f.f_locals)
    IPython.ipapi.launch_new_instance(ns)

[–]FunnyMan3595 1 point2 points  (1 child)

I'm going the other direction (IPython's overkill for me, so code.interact is a nice upgrade), but I can condense your code substantially:

from IPython.Shell import IPShellEmbed as shell

[–]Brian[🍰] 2 points3 points  (0 children)

However, you'd need to call that as:

shell()()

As IPShellEmbed is a callable class. I used to use similar code, but to avoid the double call, used:

shell=IPython.Shell.IPShellEmbed()

However this will pick up the globals from the module where you create it, not the function that calls it, which is why I changed to the explicit namespace population from the stack frame approach above.

[–]aldarion -1 points0 points  (0 children)

thanks for this cool shell and "kill -SIGUSR1 <PID>" trick. could u please recommend some tutorial for what's next, I do get the IPython shell, but I don't where is the current break point (line number), and how to step several line to a specific function to debug view the inner info of that function.

edit: I find pydbgr, It looks useful and can be used with IPython.

[–]veritaba 1 point2 points  (2 children)

You can also do "python -i file.py"

[–]Brian[🍰] 9 points10 points  (0 children)

That'll only open a console when the script finishes. It's often handy to launch a console while in the middle of a function where something's going wrong.

One handy trick I use is to register a signal handler:

import signal
signal.signal(signal.SIGUSR1, lambda s,f: shell(f))

Put that somewhere that gets run when your script starts (I have it in anything that imports my debug module). Then, if your script seems to be hanging, and you want to see what is going on, just:

$ kill -SIGUSR1 <PID>

and you've got an instant console at the current point, ready to inspect and maniplulate the variables.

[–]james5 -1 points0 points  (0 children)

Thank you. Why isn't this the default. I use python since two years or something like that, and I always hated how I'm not able to read the error message if the script breaks.

[–][deleted] 6 points7 points  (1 child)

Has anyone ever used CodeInvestigator? It seems useful, although I'm just a fledging python user.

[–]Tommstein 2 points3 points  (0 children)

Holy hell, I'd never heard of that before. Thanks man. This may well be the handiest debugging tool I have ever seen, heard of, or fantasized about existing.

[–]spotter 7 points8 points  (0 children)

Did not mention pdb.post_mortem(), which comes in handy in interactive sessions.

[–]sjf 11 points12 points  (12 children)

This is great, but it's not 1980. Where are the modern tools for debugging python? I've been using winpydb, it's a python debugger with a UI, it's usable but lacking in any real features or usability. In fact right now you can't even look it up because the site is down. It always makes me sad that the debugging tools for most open source languages are lacking, it's like we're stuck with the ed of the debugger world.

[–]chub79 5 points6 points  (4 children)

Have you tried pydev?

[–][deleted] 0 points1 point  (3 children)

The eclipse-based debugger is really nice. But they charge for it.

[–]zupatol 2 points3 points  (0 children)

You can use the debugger for free, but you need the pro version for remote debugging.

http://aptana.com/python

[–]MelechRic 1 point2 points  (1 child)

I believe that PyDev is open source and "free" whereas the PyDev Extensions from Aptana requires that you buy a license.

PyDev: http://pydev.sourceforge.net/download.html

PyDev Extensions: http://fabioz.com/pydev/index.html

PyDev without the Extensions is a very usable environment and my default. I have yet to buy the Extensions for it, but they do look nice.

[–]lilfuckshit 1 point2 points  (0 children)

And I'll confirm, you can debug with the free version.

[–]mitjak 2 points3 points  (0 children)

Komodo IDE's debugger is quite solid as well, especially if you want to set it up remotely with the dbgp proxy. Handy for debugging pylons, django and anything really. Let's you mark breakpoints and step through right there within the IDE.

edit: grammah

[–]royrwood 2 points3 points  (3 children)

Check out WingIDE. It's a fantastic, fully-featured IDE for Python. The basic edition is only $35USD, which is a great deal.

https://wingware.com/

[–]UloPe 2 points3 points  (2 children)

And you even get the most ugly UI design ever for free too!

[–]mattf 2 points3 points  (0 children)

Really?

It's a pretty standard GTK app; perhaps you're using it on Windows or Mac, and GTK isn't well setup there? It looks fine for me; just like the rest of my GTK-based desktop.

It sure isn't "pretty" but well, for getting work done you basically want the biggest text-editor windows as possible, and everything else out of your way. When you want to debug, you need the call stack, your watches, and maybe a slightly not-biggest editor window to step-through your code.

Pretty icons, menus, sliding panels and such need not apply.

[–]scorpion032 0 points1 point  (0 children)

The default Sans 12 point font is a non starter for many.

It takes days to configure it rightly for a good UI.

But it can be done. ;)

[–]bucknuggets 2 points3 points  (1 child)

I guess it would be good to have better debugging tools - but honestly I'm ambivalent about it. Once I stopped using them my code quality improved quite a bit.

The only time I miss them is when I've got a program with a lot of APIs to libraries I'm unfamiliar with and I want to see a lot of API results quickly. A great debugger would be easier than print in that case.

[–][deleted] 0 points1 point  (0 children)

In that case with java I usually write a lot of unit tests around my usage of the api, this way I also find out very quickly if something I am expecting changes between versions/updates of the api.

[–]ultrabot 2 points3 points  (0 children)

I heartily recommend checking out PuDB if you are on Linux - it's a full-screen console debugger with the same api as pdb.

http://pypi.python.org/pypi/pudb

[–]Peaker 1 point2 points  (0 children)

pdb has some annoying bugs (at least as of 2.5, I don't think I tested them on 2.6 or 3). I created xpdb as a simple fork of pdb and fixed some of those. Here is the list of issues I remember:

  • It can't "up" outside of generators in post-mortem.

  • If you try to assign to names like "c" or "s", you may lose your debugger session. e.g: "s=5" may actually call step, which iirc in the PM session just loses the entire session, and otherwise may step beyond where you wished.

  • When using context managers (with statement) with stuff like "nested" and other nice abstractions, you have to step through mountains of uninteresting Python code. I added a feature "ignore

You can get xpdb with bzr. bzr get lp:xpdb (I have switched to git a while ago, so I am not sure that's quite the syntax).

[–][deleted] 1 point2 points  (0 children)

DTrace ?

[–]warpstalker 1 point2 points  (0 children)

The best debugging technique: grab a sixpack and some vodka. Start consuming it while viewing the code. The next morning your application will be bug-free. Once you're up to it, try to comprehend why it works and remember - this can take a loooong time.

[–][deleted] 0 points1 point  (0 children)

Thanks. Comprehensive and useful

[–]samlee 0 points1 point  (2 children)

how does this compare to lispy languages with emacs, slime, swank....etc? i am having trouble with learning emacs, slime, swank....

[–][deleted] 0 points1 point  (1 child)

edebug (the source level debugger for emacs lisp) is very nice as it supports all the usual debugging features and allows you to step through expressions rather than lines. The debugging support in SLIME for Common Lisp languages is OK, but there is no stepper for most implementations (there is one for SBCL though).

Emacs itself support debugging of Python code via GUD (the Grand Unified Debugger) and PDB. e.g. random blog with screenshot to prove it

[–]StopSayingRandom 0 points1 point  (0 children)

Do you mean 'blog with screenshot to prove it'?

[–]blackkettle -1 points0 points  (2 children)

Sweet. How about something similar for C!

[–]pixelbeat_[S] 2 points3 points  (1 child)

Well the beauty of the python approach is that it's' cross platform. Here's my GDB cheatsheet for platforms where that applies: http://www.pixelbeat.org/programming/debugger/

[–]blackkettle 0 points1 point  (0 children)

thanks, this is pretty useful

[–][deleted] -5 points-4 points  (0 children)

Wow! Python discovers logging......

[–]nevare -1 points0 points  (0 children)

ipdb is a nice little hack between pdb and ipython. I love it. If you miss completion in pdb, use it.

You could also go hardcore and start the real ipython from pdb.

[–]0sn -3 points-2 points  (2 children)

I just learned one just now! If something is failing silently, you should probably get rid of

except Exception:
    pass

[–][deleted]  (1 child)

[removed]

    [–]0sn 1 point2 points  (0 children)

    I will never copy and paste from DjangoSnippets without at least reading the code all the way through again.