MyTetra Share
Делитесь знаниями!
О, смотри-ка какое хорошее место. Дайте два!
Раздел: root - Linux - Python



What are your best tips for debugging Python?

Please don't just list a particular debugger without saying what it can actually do.


python debugging


edited Sep 24 '14 at 8:43

community wiki

7 revs, 5 users 56%

locked by Brad Larson♦ Oct 5 '14 at 15:59

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

closed as too broad by devnull, Chris, Tony Hopkinson, gnat, nsfyn55 May 31 '14 at 19:31

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question. –  osa Sep 28 '14 at 2:29

comments disabled on deleted / locked posts / reviews

18 Answers

active oldest votes

139 accepted


You can use the pdb module, insert pdb.set_trace() anywhere and it will function as a breakpoint.

>>> import pdb

>>> a="a string"

>>> pdb.set_trace()


> <stdin>(1)<module>()->None

(Pdb) p a

'a string'


To continue execution use c (or cont or continue).

It is possible to execute arbitrary Python expressions using pdb. For example, if you find a mistake, you can correct the code, then type a type expression to have the same effect in the running code

ipdb is a version of pdb for IPython. It allows the use of pdb with all the IPython features including tab completion.

It is also possible to set pdb to automatically run on an uncaught exception.

Pydb was written to be an enhanced version of Pdb. Benefits?


edited Feb 4 '10 at 14:57

community wiki

8 revs, 4 users 48%



Here is an article on using pdb: –  sontek Oct 29 '10 at 5:28



Personally, I like ipdb better. –  Sardathrion Nov 15 '12 at 13:51



Apparently there's a rewrite of pydb called pydbgr –  Ehtesh Choudhury May 17 '13 at 4:13



SublimeText has a great plugin to add python breakpoints to code: –  Denis Golomazov Sep 1 '14 at 10:56



If you are developing a web application, add a view for in debug mode that simply does import pdb; pdb.set_trace(). If you are using Flask/Werkzeug that has an interactive debugger, you can also have a view that just does assert False. –  osa Sep 28 '14 at 2:32

add a comment

78, a full-screen, console-based Python debugger.

Its goal is to provide all the niceties of modern GUI-based debuggers in a more lightweight and keyboard-friendly package. PuDB allows you to debug code right where you write and test it – in a terminal. If you've worked with the excellent (but nowadays ancient) DOS-based Turbo Pascal or C tools, PuDB's UI might look familiar.

Nice for debugging standalone scripts, just run

python -m


answered Jun 20 '11 at 22:46

community wiki




Wow, that is freakin' sweet! –  kindall Jun 20 '11 at 23:07



This just made my day! Great find. –  Burhan Khalid Dec 10 '13 at 5:23



Install with pip install pudb –  congusbongus Jul 31 '14 at 1:27

add a comment


If you are using pdb, you can define aliases for shortcuts. I use these:

# Ned's .pdbrc

# Print a dictionary, sorted. %1 is the dict, %2 is the prefix for the names.

alias p_ for k in sorted(%1.keys()): print "%s%-15s= %-80.80s" % ("%2",k,repr(%1[k]))

# Print the instance variables of a thing.

alias pi p_ %1.__dict__ %1.

# Print the instance variables of self.

alias ps pi self

# Print the locals.

alias pl p_ locals() local:

# Next and list, and step and list.

alias nl n;;l

alias sl s;;l

# Short cuts for walking up and down the stack

alias uu u;;u

alias uuu u;;u;;u

alias uuuu u;;u;;u;;u

alias uuuuu u;;u;;u;;u;;u

alias dd d;;d

alias ddd d;;d;;d

alias dddd d;;d;;d;;d

alias ddddd d;;d;;d;;d;;d


answered Oct 26 '09 at 10:16

community wiki

Ned Batchelder



How do you define these alias? –  Casebash Oct 26 '09 at 10:55



Wow, that could be incredibly powerful –  Casebash Oct 26 '09 at 10:55



Put this stuff in ~/.pdbrc –  Ned Batchelder Oct 26 '09 at 15:25



on windows you can put it in ~/_ipython/ipythonrc.ini –  fastmultiplication Aug 1 '10 at 5:34

add a comment



Python already has an excellent built-in logging module. You may want to use the logging template here.

The logging module lets you specify a level of importance; during debugging you can log everything, while during normal operation you might only log critical things. You can switch things off and on.

Most people just use basic print statements to debug, and then remove the print statements. It's better to leave them in, but disable them; then, when you have another bug, you can just re-enable everything and look your logs over.

This can be the best possible way to debug programs that need to do things quickly, such as networking programs that need to respond before the other end of the network connection times out and goes away. You might not have much time to single-step a debugger; but you can just let your code run, and log everything, then pore over the logs and figure out what's really happening.

EDIT: The original URL for the templates was:

This page is missing so I replaced it with a reference to the snapshot saved at

In case it disappears again, here are the templates I mentioned. This is code taken from the blog; I didn't write it.

import logging

import optparse

LOGGING_LEVELS = {'critical': logging.CRITICAL,

'error': logging.ERROR,

'warning': logging.WARNING,

'info': logging.INFO,

'debug': logging.DEBUG}

def main():

parser = optparse.OptionParser()

parser.add_option('-l', '--logging-level', help='Logging level')

parser.add_option('-f', '--logging-file', help='Logging file name')

(options, args) = parser.parse_args()

logging_level = LOGGING_LEVELS.get(options.logging_level, logging.NOTSET)

logging.basicConfig(level=logging_level, filename=options.logging_file,

format='%(asctime)s %(levelname)s: %(message)s',

datefmt='%Y-%m-%d %H:%M:%S')

# Your program goes here.

# You can access command-line arguments using the args variable.

if __name__ == '__main__':


And here is his explanation of how to use the above. Again, I don't get the credit for this:

By default, the logging module prints critical, error and warning messages. To change this so that all levels are printed, use:

$ ./ --logging=debug

To send log messages to a file called debug.log, use:

$ ./ --logging-level=debug --logging-file=debug.log


edited May 1 '13 at 5:24

community wiki

4 revs, 2 users 91%



The problem with logging module is that it heavily breaks with Unicode and various workarounds are needed to have it working within an internationalized applications. Though, this is still the best logging solution for Python. –  Jacek Konieczny Feb 26 '10 at 12:15



The link "logging template here" is dead. Please update. –  Yohann Mar 22 '13 at 8:56

add a comment


It is possible to print what Python lines are executed (thanks Geo!). This has any number of applications, for example, you could modify it to check when particular functions are called or add something like ## make it only track particular lines.

code.interact takes you into a interactive console

import code; code.interact(local=locals())

If you want to be able to easily access your console history look at: "Can I have a history mechanism like in the shell?" (will have to look down for it).

Auto-complete can be enabled for the interpreter.


edited Feb 26 '10 at 9:20

community wiki

4 revs

add a comment


ipdb is like pdb, with the awesomeness of ipython.

← Содержание ...
MyTetra Share v.0.35
Яндекс индекс цитирования