In general, Pycharm is used to debug projects, but what happens when it's not convenient? Do you still use the old logger? Today, it's super convenient to find pysnooper's tool, which can type the output of every step of a function into a file.
- First, you need to install it.
pip install pysnooper
- Then, using it, pysnooper can be placed on the function you want debug as a decorator, and then specify the output file, or it can call the console directly without output.
import pysnooper @pysnooper.snoop('test.log') def main(): a = 1 b = 5 while a < b: a += 1 if __name__ == '__main__': main()
The output is as follows:
11:16:41.756880 call 4 def main(): 11:16:41.757319 line 5 a = 1 New var:....... a = 1 11:16:41.757601 line 6 b = 20 New var:....... b = 20 11:16:41.757867 line 7 while a < b: 11:16:41.758004 line 8 a += 1 Modified var:.. a = 2 11:16:41.758232 line 7 while a < b: 11:16:41.758363 line 8 a += 1 Modified var:.. a = 3 11:16:41.758583 line 7 while a < b: 11:16:41.758714 line 8 a += 1 Modified var:.. a = 4 11:16:41.758932 line 7 while a < b: 11:16:41.759058 line 8 a += 1 Modified var:.. a = 5 11:16:41.759277 line 7 while a < b: 11:16:41.759404 line 8 a += 1 Modified var:.. a = 6 11:16:41.759623 line 7 while a < b: 11:16:41.759750 line 8 a += 1 Modified var:.. a = 7 11:16:41.759968 line 7 while a < b: 11:16:41.760095 line 8 a += 1 Modified var:.. a = 8 11:16:41.760312 line 7 while a < b: 11:16:41.760437 line 8 a += 1 Modified var:.. a = 9 11:16:41.760653 line 7 while a < b: 11:16:41.760778 line 8 a += 1 Modified var:.. a = 10 11:16:41.760994 line 7 while a < b: 11:16:41.761119 line 8 a += 1 Modified var:.. a = 11 11:16:41.761336 line 7 while a < b: 11:16:41.761462 line 8 a += 1 Modified var:.. a = 12 11:16:41.761682 line 7 while a < b: 11:16:41.761809 line 8 a += 1 Modified var:.. a = 13 11:16:41.762026 line 7 while a < b: 11:16:41.762151 line 8 a += 1 Modified var:.. a = 14 11:16:41.762402 line 7 while a < b: 11:16:41.762620 line 8 a += 1 Modified var:.. a = 15 11:16:41.762853 line 7 while a < b: 11:16:41.762983 line 8 a += 1 Modified var:.. a = 16 11:16:41.763217 line 7 while a < b: 11:16:41.763353 line 8 a += 1 Modified var:.. a = 17 11:16:41.763564 line 7 while a < b: 11:16:41.763687 line 8 a += 1 Modified var:.. a = 18 11:16:41.763918 line 7 while a < b: 11:16:41.764040 line 8 a += 1 Modified var:.. a = 19 11:16:41.764245 line 7 while a < b: 11:16:41.764364 line 8 a += 1 Modified var:.. a = 20 11:16:41.764612 line 7 while a < b: 11:16:41.764730 return 7 while a < b: Return value:.. None
- Of course, if you don't want to debug the whole function, you can also specify dubug blocks inside the function.
import pysnooper def main(): a = 1 b = 5 c = 0 while a < b: a += 1 with pysnooper.snoop('test2.log'): c += a if __name__ == '__main__': main()
The output is as follows:
New var:....... a = 2 New var:....... b = 5 New var:....... c = 0 11:16:07.802545 line 11 c += a New var:....... a = 3 New var:....... b = 5 New var:....... c = 2 11:16:07.803153 line 10 with pysnooper.snoop('test2.log'): 11:16:07.803323 line 11 c += a New var:....... a = 4 New var:....... b = 5 New var:....... c = 5 11:16:07.803789 line 10 with pysnooper.snoop('test2.log'): 11:16:07.804074 line 11 c += a New var:....... a = 5 New var:....... b = 5 New var:....... c = 9 11:16:07.804646 line 10 with pysnooper.snoop('test2.log'): 11:16:07.804821 line 11 c += a
As if these two are enough for everyday use, if you want to use some more advanced usage, see: https://github.com/cool-RR/pysnooper