2017-02-14 53 views
0

我想要更熟練地使用調試器,並遵循http://www.onlamp.com/pub/a/python/2005/09/01/debugger.html中給出的示例。目前,我想要這個腳本:使用Python調試器時發生NameError(ipdb)

#!/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)") 

但是,如果我運行它,並嘗試按n,我得到一個NameError

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

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

當我從https://docs.python.org/2/library/pdb.html#pdb.run理解,它應該是可能的使用n(ext)命令運行,直到實際的錯誤。有人可以解釋這裏發生了什麼嗎?

+1

有'pdb'和'ipdb'之間的差異? – WhatsThePoint

回答

1

從您提到的文檔中,解釋鏈接到https://docs.python.org/2/library/functions.html#eval

看來,您的來電ipdb.run()不提供globalslocals字典,所以test_debugger是不是在run上下文中定義。

你可以把它像這樣工作的:

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