python如何调试?
在执行shell脚本的时候,可以sh -x来显示程序运行过程,可以用来排错
在python中是否有呢?答案是肯定的
1.可以使用pdb来进行调试,举例如下
代码及运行结果如下:
[root@node100 vagrant]# cat kwargs.py #!/usr/bin/python def test_kwargs(farg,*argvs,**kwargs): print "farg is " + farg for arg in argvs: print "another arg is " + arg for key ,value in kwargs.items(): print key + " is " + value test_kwargs("first","second","third",name="zhaolibin",age="18") [root@node100 vagrant]# python kwargs.py farg is first another arg is second another arg is third age is 18 name is zhaolibin
使用pdb调试可以如下:
[root@node100 vagrant]# python -m pdb kwargs.py > /home/vagrant/kwargs.py(2)<module>() -> def test_kwargs(farg,*argvs,**kwargs): (Pdb) s > /home/vagrant/kwargs.py(8)<module>() -> test_kwargs("first","second","third",name="zhaolibin",age="18") (Pdb) s --Call-- > /home/vagrant/kwargs.py(2)test_kwargs() -> def test_kwargs(farg,*argvs,**kwargs): (Pdb) s > /home/vagrant/kwargs.py(3)test_kwargs() -> print "farg is " + farg (Pdb) s farg is first > /home/vagrant/kwargs.py(4)test_kwargs() -> for arg in argvs: (Pdb) s > /home/vagrant/kwargs.py(5)test_kwargs() -> print "another arg is " + arg (Pdb) s another arg is second > /home/vagrant/kwargs.py(4)test_kwargs() -> for arg in argvs: (Pdb) s > /home/vagrant/kwargs.py(5)test_kwargs() -> print "another arg is " + arg (Pdb) s another arg is third > /home/vagrant/kwargs.py(4)test_kwargs() -> for arg in argvs: (Pdb) ss *** NameError: name 'ss' is not defined (Pdb) s > /home/vagrant/kwargs.py(6)test_kwargs() -> for key ,value in kwargs.items(): (Pdb) s > /home/vagrant/kwargs.py(7)test_kwargs() -> print key + " is " + value (Pdb) s age is 18 > /home/vagrant/kwargs.py(6)test_kwargs() -> for key ,value in kwargs.items(): (Pdb) s > /home/vagrant/kwargs.py(7)test_kwargs() -> print key + " is " + value (Pdb) s name is zhaolibin > /home/vagrant/kwargs.py(6)test_kwargs() -> for key ,value in kwargs.items(): (Pdb) s --Return-- > /home/vagrant/kwargs.py(6)test_kwargs()->None -> for key ,value in kwargs.items(): (Pdb) s --Return-- > /home/vagrant/kwargs.py(8)<module>()->None -> test_kwargs("first","second","third",name="zhaolibin",age="18")
2.常用的命令如下:
c 继续执行
w 显示当前正在执行的代码行的上下文
a 打印当前函数的参数列表
s 执行当前代码行,并停在第一个能停的地方(相当于单步进入)
n 继续执行到当前函数的下一行,或者当前行直接返回(单步跳过)
一般用s就可以了