Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError when using the Python debugger (ipdb)

Tags:

python

pdb

ipdb

I'm trying to become more adept at using the debugger, and am following the examples given in http://www.onlamp.com/pub/a/python/2005/09/01/debugger.html. I'm currently trying this script:

#!/usr/bin/env python

import ipdb

def test_debugger(some_int):
    print "start some int>>", some_int
    return_int = 10 / some_int
    print "end some_int>>", some_int
    return return_int

if __name__ == "__main__":
    ipdb.run("test_debugger(0)")

However, if I run it and try to press n, I get a NameError:

> <string>(1)<module>()

ipdb> n
NameError: "name 'test_debugger' is not defined"

As I understand from https://docs.python.org/2/library/pdb.html#pdb.run, it should be possible to use the n(ext) command to run until the actual bug. Can someone explain what is going on here?

like image 356
Kurt Peek Avatar asked Sep 09 '25 23:09

Kurt Peek


1 Answers

From the docs you mention, the explanation links to https://docs.python.org/2/library/functions.html#eval.

It seems that your call to ipdb.run() does not provide a globals or locals dict, so test_debugger is not defined in the context of run.

You can make it work like this:

ipdb.run("test_debugger(0)", {'test_debugger': test_debugger})

like image 148
Laurent S Avatar answered Sep 12 '25 14:09

Laurent S