In daily development , There will be some pain points , Conventional methods cannot debug, Such as :
The production environment is not completely consistent with the test environment , In the test environment , Some scenes cannot be reproduced
The program log is not detailed enough
You need to quickly define problems through interactive debugging , And you need to try to change the value of the variable to reproduce
Obviously , It's not a special moment , No need to consider remote debug In the form of .
PyCharm Provide remote debug, But it's heavier , Here we go through remote-pdb(https://github.com/ionelmc/python-remote-pdb) To achieve remote debug.
First of all, let's get to know remote-pdb, Everyday we develop py when , Will use pdb debug , but pdb It does not provide the function of remote debugging , The developer is based on pdb Developed pdbx,pdbx Can be pdb Debugging information passed telnet or nc Command delivery , So as to achieve the effect of remote debugging ,remote-pdb stay pdbx above , Another layer of encapsulation , It will be more convenient to use .
install remote-pdb
pip install remote-pdb
We go through Python Write a Fibonacci sequence code :
from remote_pdb import RemotePdb
rpdb = RemotePdb('127.0.0.1', 6666)
def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
# The breakpoint
rpdb.set_trace()
return a
print (fib(10))
In the above code , Introduced remote_pdb Medium RemotePdb Class and instantiate rpdb, We can follow up by telnet or nc Command to link RemotePdb Provided TCP service .
Here we choose telnet, If you are windows System , You need to open before telnet service , stay windows In the set , find 【 Start or close windows function 】, And find it in there telnet Relevant stuff , Just turn it on .
Run the program with the breakpoint , And then through 【telnet 127.0.0.1 6666】 De link , You will enter the attribute pdb Interactive environment , Then you can go through pdb To query the information in the program , Here's the picture :
Very simple technique , Last one PDB Command diagram for .
I'm two liang , See you next article .