How to efficiently debug Python programs?

Source: Internet
Author: User
Now I can print out the debugpython program in a possible error. I don't know if python has one-step debugging tools like c ++'s IDE? Or how do python programmers debug their own python programs? Now I can print the python program in debug for a while where possible errors occur. I wonder if python has one-step debugging tools like c ++'s IDE? Or how do python programmers debug their own python programs? Reply content: Please be angry with me.
Should I have used IPython? Imagine how happy it is to automatically bring you to IPython Shell when an exception is thrown? In addition, unlike IPython, you can call commands such as p (print), up (up stack), and down (down stack. You can also create temporary variables and execute any function.

In fact, this can be implemented, and it is very simple, but you need to install IPython first. Save the following code as 'crash _ on_ipy.py'

import sysclass ExceptionHook:    instance = None    def __call__(self, *args, **kwargs):        if self.instance is None:            from IPython.core import ultratb            self.instance = ultratb.FormattedTB(mode='Plain',                 color_scheme='Linux', call_pdb=1)        return self.instance(*args, **kwargs)sys.excepthook = ExceptionHook()
To use gdb-like functions, you can use pdb, for example:

Import pdb
Pdb. set_trace ()

Add the above two lines to the Code where a breakpoint needs to be added. During running, the execution can be interrupted. Single-step, continuous, and variable value viewing functions are available. You may wish to help. Recommended PyCharm
Good IDE, designed for python ~ , Supports win, linux, maxos
Supports breakpoint debugging and built-in connectors (GitHub, Mantis, Jira, and Bugzilla)
Multi-language hybrid programming is better than eclipse.
The interface is shown in the following figure:

Personal experience is good, but in the macos system, configuring interpreter requires a little effort.
Visit the official website: http://www.jetbrains.com/pycharm/
A picture is better than a thousand words. The upper left is the call stack, the upper right is the variable value, and the bottom is the code currently run to that line, which is much more efficient than debugging methods such as print.

Using eclipse + pydevpython-m pdb your. py I recently used pudb, and it feels more friendly than pdb. The command line installation in linux is simple:

pip install pudb
Print + type always goes ~ It must be ipython. The best time to debug a program is when a program fails. Sometimes, when an error occurs, you can call the debugger to reproduce it again. It may take a long time to reproduce the error. Running a program in ipython can debug the program immediately when an error occurs. It is absolutely convenient to view the content in the stack. I tried the py component of vs2015 and felt that it was no different from pycharm in vim or textmate. debug print. dir and type. If you don't understand it, you can use pdb ..

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.