Fork me on GitHub

Python -- 调试


调试

1 print()

def foo(s):
    n = int(s)
    print('>>> n = %d' % n)
    return 10 / n

 

2 assert

def foo(s):
    n = int(s)
    assert n != 0, 'n is zero!'
    return 10 / n

 

3 logging

import logging

logging.basicConfig(level=logging.INFO)     #可以指定记录信息的级别,有debuginfowarningerror

s = '0'
n = int(s)
logging.info('n = %d' % n)
print(10 / n)

 

4 pdb 

# err.py
s = '0'
n = int(s)
print(10 / n)

$python -m pdb err.py
   l: 查看代码
   n: 单步执行代码
   p 变量名: 查看变量
   q: 退出调试

 

5 pdb.set_trace()

# err.py
import pdb

s = '0'
n = int(s)
pdb.set_trace() # 运行到这里会自动暂停进入pdb调试环境,可以用命令p查看变量,或者用命令c继续运行:
print(10 / n)

 

6 IDE

  PyCharm,  Spyder,  Eclipse+pydev

 

posted @ 2015-10-23 13:52  Roronoa__Zoro  阅读(128)  评论(0编辑  收藏  举报