Debugging flask with pdb Debugging flask with pdb flask flask

Debugging flask with pdb


You can do this at the line where you want execution to break:

import pdb; pdb.set_trace()

Just make sure you delete it before you commit :).


I'm trying to use pdb to debug flask application. Setting break point is easy; I just use b index to break when index() is invoked or b 44 to set a break point at line 44.

Yes, that's fine.

Breakpoint works with b 44 which is the start of the main, but b index doesn't work. In the command line, "Index is called" is printed to indicate that the method is invoked, but it does not stop in the pdb.

The "problem" here is that you are telling the debugger to break at the start of the function called main() but that's not the function you think it is, you'll see, what is really going on is that the decorator is replacing your main() function with some other function (flask's route handler) so when you do b index you are telling the debugger to stop on the first line of the function pointed by main, which is in flask's code.

Try setting b index1 in this example:

def deco(fn):    def _wrapper():        print "Deco called"        return fn()    return _wrapper@decodef index1():    print "Index is called"    return "hi stranger!"salva = index1if __name__ == '__main__':    import pdb; pdb.set_trace()    index1()


You can define an http endpoint which will place you inside pdb when you hit it in your browser (e.g. at http://127.0.0.1:5000/pdb:

@app.route('/pdb')def pdb():   """Enter python debugger in terminal"""   import sys   print("\n'/pdb' endpoint hit. Dropping you into python debugger. globals:")   print("%s\n" % dir(sys.modules[__name__]))   import pdb; pdb.set_trace()   return 'After PDB debugging session, now execution continues...'

You should of course add appropriate security protections as needed, e.g. disabling in production, adding authentication, etc.