Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging flask with pdb

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.

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.

@app.route('/', methods=['GET', 'POST'])
def index():
    print "Index is called"
    name = None
    ...
    return render_template('index.html', form=form, name=name)

if __name__ == '__main__':
    manager.run() # line 44

What might be wrong?

like image 511
prosseek Avatar asked Nov 08 '14 00:11

prosseek


People also ask

How do I debug Python code using pdb?

To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

How do you set a breakpoint in pdb?

Optionally, you can also tell pdb to break only when a certain condition is true. Use the command b (break) to set a breakpoint. You can specify a line number or a function name where execution is stopped. If filename: is not specified before the line number lineno , then the current source file is used.

Does pdb work with threads?

When pdb runs in a thread all other threads cannot execute python code. They can execute native code bit there is not much we can do about it. * Switching threads is the interesting one. The easiest way is to set the pdb trace function in the target thread and run until they thread executes python code.


2 Answers

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 :).

like image 86
David Sanders Avatar answered Sep 17 '22 09:09

David Sanders


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

@deco
def index1():
    print "Index is called"
    return "hi stranger!"

salva = index1

if __name__ == '__main__':
    import pdb; pdb.set_trace()
    index1()
like image 44
Joaquin Sargiotto Avatar answered Sep 21 '22 09:09

Joaquin Sargiotto