Python Cookbook (3rd edition) Chinese version: 14.12 Debugging Basic program crash error

Source: Internet
Author: User
Tags stdin

14.12 Debugging a basic program crash error?

How do you debug a program after it crashes?

Solution?

If your program crashes because of an exception, run python3 -i someprogram.py simple debugging.
-iOption to open an interactive shell after the program finishes.
Then you can look at the environment, for example, assuming you have the following code:

# sample.pyfunc(ntenfunc(' Hello ')       

python3 -i sample.pythe run will have output similar to the following:

Bash%Python3-ISample.PyTraceback(MostRecentPagerLast):File"Sample.py",Line6in <module > func ( ' Hello ' ) file  "sample.py" line 4in func return n + Span class= "Mi" >10typeerror: can ' t convert ' Span class= "NB" >int "object to str implicitly>>> func (10) 20>>>     

If you don't see the above, you can open the Python debugger after the program crashes. For example:

PDBpdb.  PM()> sample.py (4) func (),return n + ten(Pdb) W sample.py (6) <module> () Func (' Hello ')> sample.py (4) func (),return n + ten(PDB) print n' Hello '(PDB) Q> >>              

If your code is in an environment where it is difficult to get an interactive shell (for example, on a server),
You can usually catch an exception after you print the trace information. For example:

 import tracebackimport sys try: func (arg) Span class= "K" >except: print () traceback. Print_exc (file=sys. Stderr)              

If your program doesn't crash and just produces some results that you don't understand,
It's print() also a good idea to insert a statement where you're interested.
However, if you are going to do this, there are a few tips to help you.
First, the traceback.print_stack() function will create a tracking stack when your program runs to that point. For example:

>>>DefSample(N):...IfN>0:...Sample(N-1)...Else: ... traceback. Print_stack (file=sys. Stderr) >>> sample (5)  File "<stdin>", line 5, in Sample>>>        

Alternatively, you can use the pdb.set_trace() manual start debugger anywhere, as follows:

PDBfunc(argpdb).  Set_trace       ...

This is useful when the program is relatively large and you want to debug the control flow and function parameters.
For example, once the debugger starts running, you can use it print to observe variable values or tap a command such as w to get tracking information.

Discuss?

Don't make debugging too complicated. Some simple bugs just need to see the program stack information,
The actual error is generally the last line of the stack.
When you are developing, you can also insert them where you need to debug.print()
function to diagnose the information (just delete these print statements when the last release is needed).

A common use of the debugger is to observe a variable in a function that has crashed.
Knowing how to enter the debugger after a function crash is a useful skill.

When you want to dissect a very complex program, the underlying control logic is not very clear when you
It is pdb.set_trace() useful to insert such a statement.

In fact, the program runs until it touches set_trace() the statement position and then immediately enters the debugger.
Then you can do more.

If you use the IDE for Python development, the IDE will usually provide its own debugger instead of the PDB.
More information on this can be found in the IDE manuals you use.

Albert (http://www.aibbt.com/) The first artificial intelligence portal in China

Python Cookbook (3rd edition) Chinese version: 14.12 Debugging Basic program crash error

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.