Python logging for debug [copy a dude]

Source: Internet
Author: User
---------------------------------------
Import Sys
...
If__ Name__= '_ Main __':
Oldstdout = Sys. Stdout
Sys. Stdout= Open("Script. log", "W + ")
PrintScript. schoolsong (download ())
Sys. Stdout= Oldstdout
----------------------------------------

Use PDB for debugging.

Python comes with a debugger called PDB, which is similar to gnu gbd. The following uses a simple program to demonstrate the functions of PDB. The program code is as follows:

#!/usr/bin/python

import pdb
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print final

The program has imported the PDB module and added the PDB. set_trace () tracking point in the code. Now let's run the program.

Localhost :~ /Python/PDB # Python pdbtest. py
-- Return --
>/Usr/lib/python2.3/PDB. py (992) set_trace ()-> none
-> PDB (). set_trace () # execution starts from the tracking point
(PDB) N # N read the next line of code
>/Root/Python/PDB/pdbtest. py (6 )? ()
-> B = "BBB"
(PDB) N
>/Root/Python/PDB/pdbtest. py (7 )? ()
-> C = "CCC"
(PDB) p B # P print the variable value
'Bbb'
(PDB) L # l display the current execution position
2
3 Import PDB
4 A = "AAA"
5 PDB. set_trace ()
6 B = "BBB"
7-> C = "CCC"
8 Final = A + B + C
9 print final
10
[EOF]
(PDB) N
>/Root/Python/PDB/pdbtest. py (8 )? ()
-> Final = A + B + C
(PDB) N # If the command is the same as the previous one, press enter without entering 'n'
>/Root/Python/PDB/pdbtest. py (9 )? ()
-> Print final
(PDB) N
Aaabbbccc
-- Return --
>/Root/Python/PDB/pdbtest. py (9 )? ()-> None
-> Print final
(PDB) p a, B, C, final
('Aaa', 'bbb ', 'ccc', 'aaabbbccc ')
(PDB)
('Aaa', 'bbb ', 'ccc', 'aaabbbccc ')
(PDB) N
Localhost :~ /Python/PDB # Return Shell

There are many other PDB commands. You can use the help command to list all the PDB commands, and use help p to query the description of the p command.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.