python3.7调试的实例方法
作者:晓曦&sea 时间:2022-09-22 17:16:24
PyCharm IDE 窗口布局
PyCharm 调试代码实例(这里我以自己的代码为例)
__author__ = 'lxm'
#!/usr/bin/python
import thread
import time
# Define a function for the thread
def print_time( threadName, delay):
count = 0
while count < 5:
count += 1
print "%s: %s" % ( threadName, time.ctime(time.time()) )
def check_sum(threadName,valueA,valueB):
print "to calculate the sum of two number her"
result=sum(valueA,valueB)
print "the result is" ,result;
def sum(valueA,valueB):
if valueA >0 and valueB>0:
return valueA+valueB
def readFile(threadName, filename):
file = open(filename)
for line in file.xreadlines():
print line
try:
thread.start_new_thread( print_time, ("Thread-1", 2, ) )
thread.start_new_thread( check_sum, ("Thread-2", 4,5, ) )
thread.start_new_thread( readFile, ("Thread-3","test.txt",))
except:
print "Error: unable to start thread"
while 1:
# print "end"
pass
在调试之前通常需要设置断点,断点可以设置在循环或者条件判断的表达式处或者程序的关键点。设置断点的方法非常简单:在代码编辑框中将光标移动到需要设置断点的行,然后直接按 Ctrl+F8 或者选择菜单"Run"->"Toggle Line Break Point",更为直接的方法是双击代码编辑处左侧边缘,可以看到出现红色的小圆点。当调试开始的时候,当前正在执行的代码会直接显示为蓝色。下图中设置了三个断点,蓝色高亮显示的为正在执行的代码。
断点设置
表达式求值:在调试过程中有的时候需要追踪一些表达式的值来发现程序中的问题,Pycharm 支持表达式求值,可以通过选中该表达式,然后选择“Run”->”Evaluate Expression”,在出现的窗口中直接选择 Evaluate 便可以查看。
Pycharm同时提供了 Variables 和 Watches 窗口,其中调试步骤中所涉及的具体变量的值可以直接在 variable 一栏中查看。
变量查看
如果要动态的监测某个变量可以直接选中该变量并选择菜单”Run”->”Add Watch”添加到 watches 栏中。当调试进行到该变量所在的语句时,在该窗口中可以直接看到该变量的具体值。
知识点扩展:
对于 python 代码的调试我们通常都是使用 IDE 自带的调试功能。但是 IDE 提供的调试功能存在局限性,例如在测试服务器上调试代码,但是又不可能在测试服务器上安装 IDE 进行调试。这时我们就可以利用下面所讲解的三个工具进行调试。
零、准备调试代码
在讲解三个调试工具前,我们先编写待调试的代码。代码很简单,就是计算两个数的商。我们在编写代码的时候故意留下了除数为 0 的 bug。
def division(start, end):
for i in range(start, end, -1):
num1 = i
num2 = i - 1
result = num1 / num2
print(result)
if __name__ == '__main__':
division(10, 0)
来源:https://www.py.cn/jishu/gaoji/19544.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pytorch 图像预处理之减去均值,除以方差的实例
![](https://img.aspxhome.com/file/2023/3/110383_0s.jpg)
Python 如何实现数据库表结构同步
详解Python中for循环的使用方法
![](https://img.aspxhome.com/file/2023/8/100918_0s.jpg)
关于python3的ThreadPoolExecutor线程池大小设置
python获取当前运行函数名称的方法实例代码
ASP UTF-8页面乱码+GB2312转UTF-8 +生成UTF-8格式的文件(编码)
解读SQL语句中要不要加单引号的问题
![](https://img.aspxhome.com/file/2023/4/120854_0s.png)
python多线程爬取西刺代理的示例代码
![](https://img.aspxhome.com/file/2023/1/124341_0s.png)
如何使用数据绑定控件实现不换页提交数据?
Dreamweaver使用快技法十三则总结
网站导航设计模式指南
基于Python爬取股票数据过程详解
![](https://img.aspxhome.com/file/2023/7/135347_0s.png)
SQL 字母数字混合型字段 按里面的数字排序
![](https://img.aspxhome.com/file/UploadPic/20104/23/1-47s.jpg)
golang图片处理库image基本操作
举例简单讲解Python中的数据存储模块shelve的用法
Python中property属性实例解析
PyTorch学习笔记之回归实战
![](https://img.aspxhome.com/file/2023/2/78752_0s.png)
Python 聊聊socket中的listen()参数(数字)到底代表什么
![](https://img.aspxhome.com/file/2023/9/64239_0s.png)
统一的品牌设计
![](https://img.aspxhome.com/file/UploadPic/20088/7/200887125533323s.jpg)
Python练习之ORM框架
![](https://img.aspxhome.com/file/2023/2/131152_0s.png)