- I have a ~/.python/sitecustomize.py file with the following:
# Start debug on Exception
import bdb import sys
def info(type, value, tb):
if hasattr(sys, 'ps1') \
or not sys.stdin.isatty() \
or not sys.stdout.isatty() \
or not sys.stderr.isatty() \
or issubclass(type, bdb.BdbQuit) \
or issubclass(type, SyntaxError):
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthook__(type, value, tb)
else:
import traceback, ipdb
# we are NOT in interactive mode, print the exception...
traceback.print_exception(type, value, tb)
print
# ...then start the debugger in post-mortem mode.
ipdb.pm()
sys.excepthook = infoIt will start ipdb automatically in case of any exception on command line called scripts.
- I use the awesome pdb emacs package for debug interactivelly during bigger bug hunts (Also for dev too... It's very a nice tool)
- Buutt... I still find the "print dance" to be my first-to-use quick tool.
edit: Fixed pasted code
And obviously any test covering that line will fail/hang.
If one doesn't have an automated test process, then I suspect one has bigger potential errors that could sneak in than an errant pdb breakpoint. I'll just assume that your release / merge process DOES include a test suite that you can add this sort of test to.
Having to change the source to fire the debugger is a dumb way of debugging after all, and doesn't allow certain things (e.g., step thru a 3rd party library). Better coach the developers on how to use the tools properly.
I guess it's because that is an example of ad-hoc (which typically misses a lot of edge conditions) tooling created to make up for language (or in this case tooling) inadequacies.
Perhaps I should add a pre-commit hook to grep for pdb.set_trace() and reject commits with that in them.
(a) the breakpoint line doesn't move around a lot between different executions, as you edit the code;
(b) you don't want to programatically invoke the debugger (i.e. if f(x): pdb.set_trace() )
(b) Pdb supports conditions with `filename:lineno, statement`. Statement will have access to local scope. E.g.:
$ python -m manage.py runserver
(Pdb) break manage.py:11, os.environ["DJANGO_SETTINGS_MODULE"] == "myproj.settings"
Breakpoint 1 at /Users/hcarvalhoalves/Projetos/myproj/manage.py:11
(Pdb) break
Num Type Disp Enb Where
1 breakpoint keep yes at /Users/hcarvalhoalves/Projetos/myproj/manage.py:11
stop only if os.environ["DJANGO_SETTINGS_MODULE"] == "myproj.settings"
Really, it does a bunch of things. I wonder why developers are unaware of it.There are a number of different routes in Django development that you may need to debug:
1. Debugging view endpoints when not using runserver (for example when testing out your actual deploy webserver). For this, none the debuggers will work, as you have no console to run through. I combat this by using winpdb that allows remote debugging.
2. Debugging either unittest based code or when using runserver, you can use the method described by hcarvalhoalves comment.
However, I still think that in lots of cases its more powerful to import in the code. With the necessary coverage in tests, it should always be picked up.
$ python -m pdb manage.py test
(Pdb) break mymodule/myfile.py:42
(Pdb) c
The above should work for tests if I understand correctly, though I haven't checked yet. This solves most of my current debugging problems, and if I'm reading correctly that would solve yours too?I wonder if you could also use it with runfcgi or any of the other manage commands? I personally use nginx+gunicorn, but for testing production I could switch it to runfcgi or similar really quick.
To be honest though, I don't find myself ever debugging production.
$ python -m pdb manage.py runserver
(Pdb) break mymodule/myfile.py:42
(Pdb) c
EDIT: For the lazy, here's a script to set breakpoints from the command line and run your scripts:
https://gist.github.com/hcarvalhoalves/7587621