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.py
will 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:
- In the
Settings-Project Interpreter
inside clickProject Interpreter
configuration on the right, click Add - select
SSH Interpreter
- If you already have remote synchronization code configuration, choose
Existing Server configuration
- Otherwise, choose
New server configuration
, set up a remote machine and ssh port IP and user name, password, etc. - Python set address of the remote machine, e.g.
/data/env/bin/python
- Settings
Sync folders
. This is the directory mapping windows and remote machines, for example,E://project
is mapped to/data/project
. If you do thatE://project/test.py
the script is equivalent to execute/data/project/test.py
script - Create test scripts,
test.py
- Open
Run-Edit Configurations
, modifytest.py
thePython interpreter
to just setRemote Python
- Click
run test.py
will be able to execute this script in a remote machine, console will display console remote machine - In addition to run, you can also debug
Reference:
Python debugging tools