Python3.7 debugging example method

PyCharm IDE window layout

PyCharm debugging code example (here I take my own code as an example)

__ author__='lxm'
#! /usr/bin/python
importthread
importtime
# Defineafunctionforthethread
defprint_time(threadName,delay):
count=0
whilecount<5:
count+=1
print"%s:%s"%(threadName,time.ctime(time.time()))defcheck_sum(threadName,valueA,valueB):
print"tocalculatethesumoftwonumberher"
result=sum(valueA,valueB)
print"theresultis",result;defsum(valueA,valueB):
ifvalueA 0andvalueB 0:
returnvalueA+valueB
defreadFile(threadName,filename):
file=open(filename)
forlineinfile.xreadlines():
printline
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:unabletostartthread"
while1:
# print"end"
pass

It is usually necessary to set breakpoints before debugging. Breakpoints can be set at loop or conditional judgment expressions or key points in the program. The method of setting a breakpoint is very simple: in the code edit box, move the cursor to the line where the breakpoint needs to be set, and then directly press Ctrl+F8 or select the menu "Run"-"Toggle Line Break Point", a more direct method is Double-click the left edge of the code editor, you can see the red dots appear. When debugging starts, the currently executing code will be directly displayed in blue. Three breakpoints are set in the figure below, and the code being executed is highlighted in blue.

Breakpoint setting

Expression evaluation: Sometimes it is necessary to track the value of some expressions to find problems in the program during debugging. Pycharm supports expression evaluation. You can select the expression and then select "Run"-"Evaluate Expression" , Directly select Evaluate in the window that appears to view it.

Pycharm also provides Variables and Watches windows, where the values of specific variables involved in the debugging steps can be viewed directly in the variable column.

Variable view

If you want to dynamically monitor a variable, you can directly select the variable and select the menu "Run"-"Add Watch" to add it to the watches column. When debugging goes to the statement where the variable is located, you can directly see the specific value of the variable in this window.

Knowledge point expansion:

For the debugging of python code, we usually use the debugging function that comes with the IDE. However, the debugging functions provided by the IDE have limitations, such as debugging code on the test server, but it is impossible to install the IDE on the test server for debugging. At this time, we can use the three tools explained below to debug.

Zero, ready to debug code

Before explaining the three debugging tools, we first write the code to be debugged. The code is very simple, just calculate the quotient of two numbers. When we wrote the code, we deliberately left a bug that divides by 0.

def division(start, end):for i inrange(start, end,-1):
 num1 = i
 num2 = i -1
 result = num1 / num2
 print(result)if __name__ =='__main__':division(10,0)

Recommended Posts

Python3.7 debugging example method
Python right alignment example method
Python object-oriented example
Example method of reading sql from python
Python example method to open music files
Python magic method topic
Python object-oriented magic method
Python function-dictionary get() method
Python error handling method
Python interpolate interpolation example
Python defines a function method
Python TCP packet injection method
Python drawing ring graph method
Python tracking except information method
Python negative modulus operation example
Python3 logging log package example
Python regular expression example code
Python output mathematical symbols example
Python iterable object de-duplication example
Python implements gradient descent method
Python one-dimensional two-dimensional interpolation example
Python draw bar graph (bar graph) example
Python method of parameter passing
Python arithmetic sequence calculation method
Method steps to increase python font
Summary of logarithm method in Python
Python regular expression learning small example
Python and js interactive call method
Python calculation of information entropy example
Python waterfall line indicator writing example
Python list comprehension operation example summary
Python ATM function implementation code example
Python example to calculate IV value
Python implements the steepest descent method
Python decorator simple usage example summary
Example operation of python access Alipay
Python tcp transmission code example analysis
End the method of running python
Python requests module session code example
Python verification code interception identification code example
Python crawler example to get anime screenshots
Method of installing django module in python
python implements the gradient method python the fastest descent method
Example of python calculating derivative and plotting
The specific method of python instantiation object
Python Romberg method to find integral examples
The specific method of python import library
Python replacement pip source method process analysis