Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

How to debug in Python?

Answer:

Assume you have a simple python script like:

a = 1
b = 2
c = a + b
print c

To enable debugging, add the following line at the top of the program

import pdb

and in the line you want to break, add

pdb.set_trace()

So the whole program become:

import pdb
a = 1
b = 2
pdb.set_trace()
c = a + b
print c

When you execute the script by python test.py, you will in the debug mode.

Some useful commands:

1. Print variables: p a
2. Step over: n
3. Continue: c
4. Quit: q

  1. How to perform syntax check for Python program
  2. Write Python script using utf-8
  3. How do I find the current module name in Python?
  4. Render web page using PyQt
  5. Multi-line string in Python

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>