官术网_书友最值得收藏!

Within a Python script

The previous two techniques will start the debugger at the beginning of a Python program. But this third technique is best for long-running processes. To start the debugger within a script, use set_trace().

Now, modify your pdb_example.py file as follows:

import pdb
class Student:
def __init__(self, std):
self.count = std

def print_std(self):
for i in range(self.count):
pdb.set_trace()
print(i)
return

if __name__ == '__main__':
Student(5).print_std()

Now, run the program as follows:

student@ubuntu:~$ python3 pdb_example.py
> /home/student/pdb_example.py(10)print_std()
-> print(i)
(Pdb) continue
0
> /home/student/pdb_example.py(9)print_std()
-> pdb.set_trace()
(Pdb)

set_trace() is a Python function, therefore you can call it at any point in your program.

So, these are the three ways by which you can start a debugger.

主站蜘蛛池模板: 东丰县| 苍梧县| 长治市| 本溪市| 万全县| 阿图什市| 河南省| 高碑店市| 台北县| 高尔夫| 甘肃省| 陆川县| 昂仁县| 广饶县| 保定市| 县级市| 江达县| 新昌县| 白朗县| 呼图壁县| 杭锦后旗| 凤庆县| 遂溪县| 抚松县| 巴彦县| 英山县| 和硕县| 吕梁市| 连城县| 临清市| 屏南县| 大名县| 罗甸县| 周宁县| 盈江县| 西昌市| 呼伦贝尔市| 永嘉县| 海丰县| 北安市| 寻甸|