Python debugging tools

1. Log

To print the variables log or print. If necessary, can be printed locals()andglobals()

Recommended logging.debug () instead of print, so the formal environment, can be unified delete these logs.

2.trace

Python has a trace mode, you can print the entire program execution flow

# encoding=utf8
def f():
    print 'aa'


if __name__ == '__main__':
    f()

For example, this simple program, execution python -mtrace --trace test.pywill log the following output:

 --- modulename: test, funcname: <module>   # test模块,module级函数
test.py(2): def f():   #test.py的第2行,执行def f命令定义函数
test.py(6): if __name__ == '__main__':
test.py(7):     f()
 --- modulename: test, funcname: f
test.py(3):     print 'aa'
aa
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

You can get to see the detailed code execution flow generation in Python.

When the code is more complex, such as calling a number of third-party libraries, the trace log will be very large, so it is best to redirect to a file inside, slowly analysis.

3.PDB

# encoding=utf8
import pdb
def f2():
    a = 1
    pdb.set_trace()  # 设置断点
    b = 2
    c = a + b
    pdb.set_trace()


if __name__ == '__main__':
    f2()

Set a breakpoint in the code inside. The implementation of the code python -mpdb test.py, you can debug the program. The process will pause at the breakpoint, waiting for us to execute the command

(vsing_env) [root@www script]# python -mpdb  test.py              
> /data/selfmoe_backend/script/test.py(2)<module>()
-> import pdb
(Pdb) c  
> /data/selfmoe_backend/script/test.py(13)f2()
-> b=2
(Pdb) p a
1
(Pdb) l
  8         cli=redis.Redis(host='127.0.0.1',port='6801')
  9         cli.get('test')
 10     def f2():
 11         a=1
 12         pdb.set_trace()
 13  ->     b=2
 14         c=a+b
 15         pdb.set_trace()
 16     if __name__ == '__main__':
 17         f2()
[EOF]

Breakpoint commands that can be executed are:

  • c continue continue
  • q quit quit
  • l list display before and after the break point source
  • W or where the process execution back to the current breakpoint
  • d down backtracking Next
  • u up backtracking Previous
  • Enter to repeat the last command, for example, would not have been by c, you can just press Enter

Other branches

  • ipdb pdb color on the basis of increased automatically filled
  • pudb gui version
  • winpdb remote debugging, the debugging windows linux process

Above several libraries and interfaces are the same pdb.

4.Pycharm

Pycharm use this IDE for debugging. This feature is more powerful.

Pycharm also supports remote debugging. That is, windows interface operation, a process running on linux.

the way is:

  1. In the Settings-Project Interpreterinside click Project Interpreterconfiguration on the right, click Add
  2. selectSSH Interpreter
  3. If you already have remote synchronization code configuration, chooseExisting Server configuration
  4. Otherwise, choose New server configuration, set up a remote machine and ssh port IP and user name, password, etc.
  5. Python set address of the remote machine, e.g./data/env/bin/python
  6. Settings Sync folders. This is the directory mapping windows and remote machines, for example, E://projectis mapped to /data/project. If you do that E://project/test.pythe script is equivalent to execute /data/project/test.pyscript
  7. Create test scripts,test.py
  8. Open Run-Edit Configurations, modify test.pythe Python interpreterto just setRemote Python
  9. Click run test.pywill be able to execute this script in a remote machine, console will display console remote machine
  10. In addition to run, you can also debug

Reference:
Python debugging tools

Guess you like

Origin www.cnblogs.com/Xjng/p/11787397.html