Everyone knows that Python is a built-in Pdb library, and it is very convenient to debug Python programs using Pdb. However, Pdb cannot be used for remote debugging and multithreading. let's take a look at how to debug Python and
Label:Debug The first method is simple and straightforward, which is to print print out the variables that might be problematic: >>> def foo (s):
n= Int (s)
print ' >>> n =%d '% n
return 10/n
>>> def main (): C6/>foo (' 0 ')
>>> main ()
>>> n = 0
Traceback (most recent call last):
File " Assertion Where print is used to assist in viewing, you can replace it with an assertion (assert): >>> def foo (s):
N=int (s)
assertn!=0, ' n is zero '
return10/n
>>> def main ():
foo (' 0 ')
>>
The Python programming language is easier for experienced developers to get started with and has powerful functions. It is an object-oriented general programming language, it helps us easily fulfill various functional requirements. Today, we will introduce the debugging methods of Python PDB.
Analysis on how Python c
You can also debug a program in Python like Gcc/gdb, by introducing a PDB module when you run a Python program (assuming that the program you want to debug is named d.py):
Copy Code code as follows:
$ VI d.py
#!/usr/bin/python
def main ():I, sum = 1, 0For I in Xrange (100):sum = sum + IPrint sum
Tags: mes Microsoft stdout enters HTTP targe nbsp not function pdb/ipdb modules /pdb/ipdb module The main role of PDB and IPDB is for single-step debugging of Python programs, and Python debugging can refer to links. Here is a simple example of using 1 Import ipdb
2
3
Pdb.set_trace () at the specified position in the code can usually be controlled by a debug switch
The code is as follows:
Import PDB
__debug__ = True
def pdb_test (ARG):if True = = __debug__:Pdb.set_trace ()For I in range (ARG):Print (i)return arg
Pdb_test (3)
Pdb.set_trace () position is broken after run, when __debug__ = False, the code runs correctly
The code is as follows:
> c:\users\plpcc\desktop\pdbtest.py (8) pdb_test ()-I in Range (ARG):(
You can also debug a program in Python like Gcc/gdb, as long as the PDB module is introduced when you run the Python program (assuming that the program you are debugging is named d.py):
The code is as follows:
$ VI d.py#!/usr/bin/python
def main ():I, sum = 1, 0For I in Xrange (100):sum = sum + IPrint sum
if __name__
Tags: order code debug CIN div TPS Debug Set final variable1 descriptionThe PDB is a Python-brought package that provides an interactive source code debugging function for Python programs. Key features include setting breakpoints, stepping through debugging, entering function debugging, viewing current code, viewing stack fragments, dynamically changing the value
specified number of rows(Pdb) J 497>/home/jchen/regression/regressionlogcmp.py (497) comparelog ()pdb.set_trace ()A (RGS), prints the parameters of the current function(Pdb) a_logger =_base =./base/mrm-8137.log_new =./new/mrm-8137.log_caseid = 5550001_tostepnum = Ten_cmpmap = {' _bcmpbinarylog ': ' true ', ' _bcmplog ': ' true ', ' _bcmpresp ': ' True '}p, one of the most useful commands to print a variabl
This article discusses how to use PDB to debug Python without convenient ide tools. Program Source code example For example, there is a program to simulate tax calculation :#! /Usr/bin/Python Def debug_demo (VAL ): If Val Print "Level 1"Print 0Elif Val Print "Level 2"Print (Val-1600) * 0.05Elif Val Print "Level 3"Print (Val-3500) * 0.10 + (3500-1600) * 0.05Els
Method One: Run python-m pdb pythonname.py
(PDB) automatically stops at the first line and waits for debugging, so you can look at the Help
(Pdb) H
Description of these key commands
> Breakpoint Settings
(Pdb) B #断点设置在本py的第10行
or (Pdb
There are several ways to debug with PDB
The main way to debug Python programs with PDB is the following three kinds! The following describes
Command line plus-m parameter
The command line launches the target program, plus the-m argument, so that the breakpoint is called before the first line of the program executes testpdb.py
The next point of this article is th
. Similarly, after specifying level=warning, debug and info will not work. This way, you can confidently output different levels of information, do not delete, and finally unified control the output of which level of information.Another benefit of logging is that with a simple configuration, a single statement can be output to a different place at the same time, such as the console and the file.
Pdb
The 4th Way is to launch the
Python's own Debug ToolPak: PDB#-*-Coding:utf-8-*-def func (num): s = num * return sif __name__ = = ' __main__ ': print ' Debug starting ... ' print ' * ' * print ' debug ending ... ' num = + s = func (num) print sThe PDB library is not referenced in a Python file and can be used when running a python
PYTHON code, especially code written by someone else, doesn't understand. What to do? In fact Python also provides a similar to the C language for debug gdb. It's called the PDB. Combined with my own study, a simple example, to do memo and compensatory learning.First, reference:1, Http://web.stanford.edu/class/physics91si/2013/handouts/Pdb_Commands.pdf2, https://
python comes with Debug ToolPak: PDB#-*-Coding:utf-8-*-def func (num): s = num * return sif __name__ = = ' __main__ ': print ' Debug starting ... ' print ' * ' * print ' debug ending ... ' num = + s = func (num) print sThe PDB library is not referenced in a Python file, and you can add parameter
former is two variables pointing to the same object.
String Method#! /Usr/bin/Python# Filename: str_methods.pyName = 'swaroop '# This is a string objectIf name. startswith ('swa '):Print 'Yes, the string starts with "SWA "'If 'A' in name:Print 'Yes, it contains the string ""'If name. Find ('war ')! =-1:Print 'Yes, it contains the string "war "'Delimiter = '_*_'Mylist = ['Brazil ', 'Russia', 'India ', 'China']Print delimiter. Join (mylist)Output$
Tags: Linux python pdbIn Linux, you can use debugging at the interface and command line, where you record command-line debugging
At the command line, use thepython27-m pdb use_infix2postfix.py to enter debug mode
>/root/py/algor/use_infix2postfix.py (7) Import types This is the first statement of the script
H (ELP)
(Pdb) HDocumented commands
The PDB is a Python-brought package that provides an interactive source code debugging feature for Python programs, including setting breakpoints, stepping through debugging, entering function debugging, viewing current code, viewing stack fragments, dynamically changing the values of variables, and more. The PDB provi
Previously used in Windows Idel with the function of debugging a Python program, under Linux has not been debugged. (Most of the time just print) look up the Internet
Method:
Copy Code code as follows:
PYTHON-M PDB a.py
a.py is a python file.
Common commands in (
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.