Patrick Altman

I am the VP of Engineering at Eldarion, Co-Founder of Amino Software, and a Mentor at jumpstart foundry.

I am equally passionate about writing code as I am about building businesses.

You really should

Posted On

Sept. 15, 2012, 10:43 a.m.

Tags

Do you have an iOS or web application that you'd like help with?

Eldarion offers lean coaching, software development, code reviews, and in some cases staff augmentation.

Give me a call at 615-300-2930 or send us an email and let's see how we can help you.

This site is hosted on

You should consider it for your Django/Python web hosting as well!

Forget print(), Use pdb Instead

How often do you use print statements to display output in your manage.py runserver process to help debug something?

This was my go-to method of debugging for a long time.

Then I was introduced to pdb.

This is so much better and useful, especially when you have situations where you have to do a lot of things in your web application in order to reproduce the conditions you are trying to debug.

Using pdb and the interactive debugger is as simple as putting:

import pdb; pdb.set_trace()

on the line that you want to start the debug session on. When you do this and you hit that line in the execution of your application, you'll see a prompt that looks like this in your manage.py runserver output:

> /Users/paltman/dev/mywebsite/mywebsite/profiles/views.py(29)profiles_list()
-> if request.user.is_superuser:
(Pdb)

From here you can issue single letter commands like n, c, or s, which stand or Next, Step Into, or Continue.

Next (n) will simply take you to the next line of execution within the current context. Step Into (s) will jump a level deeper (if it makes sense), for example it will step into a function call being made instead of just executing it and returning the results. Finally, Continue (c) will release the debugger and continue executing until a response is sent back to the browser or it encounters another breakpoint.

There is some great documentation on this feature which I recommend reading further if interested in this.

Feedback and Commentary

blog comments powered by Disqus