[Tut] Setting up powerful debugging with ipdb

There are a tremendous amount of very useful python modules out there that you can use to simplify your python development.

The other day I found myself with a bug in my event handling code, and I really wished I had a powerful debugger. I fired up pdb… and immediately remembered what a pain it is to work with pdb (Pdb is the a command line debugger built into python).

I’m afraid I’m spoiled, I really prefer IPython to the stock python shell, and I really prefer IPython’s debugger to pdb. (IPython supports a whole lot of magical things like code completion, cached input/output, and a host of other goodies! Check out the base project at ipython.org/)

So, maybe now you’re eager to get this into your own Panda projects. Don’t worry, it’s pretty darned easy.

Firstly, we’re going to install setuptools in your Panda’s python. setuptools is another useful python project, this one is designed to manage dependencies and installation of other packages. Luckily, setuptools almost literally installs itself.
The full documentation for setuptools can be found here: peak.telecommunity.com/DevCenter/setuptools
It’s not necessary to RTFM for what we’re doing, but I recommend at least skimming the manual.
The easiest way for us to install setuptools is to download this file (peak.telecommunity.com/dist/ez_setup.py) and run it with panda’s python.

I downloaded the file to my desktop on my Win7 machine, so I fired up the command line with [Win]+r cmd [Enter], and ran:

[color=red]Note: I reconstructed this from the console, it may not be exactly what you see

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Hovis>[color=blue]cd desktop

C:\Users\Hovis\Desktop>[color=blue]ppython ez_setup.py
Searching for setuptools
Reading http://pypi.python.org/simple/setuptools/
Reading http://peak.telecommunity.com/snapshots/
Best match: setuptools 0.6c12dev-r88846
Downloading http://peak.telecommunity.com/snapshots/setuptools-0.6c12dev_r88846-py2.7.egg
Processing setuptools-0.6c12dev_r88846-py2.7.egg
Moving setuptools-0.6c12dev_r88846-py2.7.egg to c:\panda3d-1.8.0\python\lib\site-packages
Removing setuptools 0.6c11 from easy-install.pth file
Adding setuptools 0.6c12dev-r88846 to easy-install.pth file
Installing easy_install-script.py script to C:\Panda3D-1.8.0\python\Scripts
Installing easy_install.exe script to C:\Panda3D-1.8.0\python\Scripts
Installing easy_install.exe.manifest script to C:\Panda3D-1.8.0\python\Scripts
Installing easy_install-2.7-script.py script to C:\Panda3D-1.8.0\python\Scripts
Installing easy_install-2.7.exe script to C:\Panda3D-1.8.0\python\Scripts
Installing easy_install-2.7.exe.manifest script to C:\Panda3D-1.8.0\python\Scripts

Installed c:\panda3d-1.8.0\python\lib\site-packages\setuptools-0.6c12dev_r88846-py2.7.egg
Processing dependencies for setuptools
Finished processing dependencies for setuptools


Now that we have setuptools installed, the rest of this is super easy.

The packages we still need to install are ipython, and ipdb. (IPython is the super-amazing-shell-of-python-awesome-sauce, and ipdb is a simple package that lets you use the IPython debugger in other projects like your Panda scripts.)
There is also an Windows-optional package called pyreadline. Other OSes already have readline support, but on Windows IPython needs this package to print color to the terminal. Color just makes things easier, so I highly recommend it.

Part of the ez_setup installation was to add the easy_install script. We’ll use that script to install the rest of these packages. You need (re)open your command line, and navigate to the Scripts/ directory under your panda’s python. In my case, this is “C:\Panda3D-1.8.0\python\Scripts”. Next, we’ll run easy_install for each of the packages we want to install. Here’s what it looks like on my machine:

[color=red]Note: I reconstructed this from the console, it may not be exactly what you see

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Hovis>cd \Panda3D-1.8.0\python\Scripts

C:\Panda3D-1.8.0\python\Scripts>[color=blue]easy_install ipython
Searching for ipython
[color=red]# Omitted text
Finished processing dependencies for ipython

C:\Panda3D-1.8.0\python\Scripts>[color=blue]easy_install pyreadline
Searching for pyreadline
[color=red]# Omitted text
Finished processing dependencies for pyreadline

C:\Panda3D-1.8.0\python\Scripts>[color=blue]easy_install ipdb
Searching for ipdb
[color=red]# Omitted text
Finished processing dependencies for ipdb

If everything went smoothly, we’re ready to use our new tools. If you’ve had any error messages while following these instructions, try pasting them into Google, or ask here. The only reason I can think of for this stuff not to work is if you’ve been mucking around in Panda’s python installation already, and in that case you’re on your own. :slight_smile:

Here’s a super simple example from the manual; I’ve added an import statement and a set_trace statement. We import ipdb so we can use it, and when the the interpreter gets to executing the set_trace line, it will open up the debugger in the python window that automatically opens every time you run your app.

from direct.showbase.ShowBase import ShowBase
import ipdb

class MyApp(ShowBase):
    def __init__(self):
        # Load the environment model.
        self.environ = self.loader.loadModel("models/environment")
        # Reparent the model to render.


        # Apply scale and position transforms on the model.
        self.environ.setScale(0.25, 0.25, 0.25)
        self.environ.setPos(-8, 42, 0)
app = MyApp()

This is what you should see in the python window.

C:\Windows\system32\cmd.exe /c ppython C:\Users\Hovis\desktop\foo.py
Known pipe types:
(all display modules loaded.)
> c:\users\hovis\desktop\foo.py(17)__init__()
     16         # Apply scale and position transforms on the model.
---> 17         self.environ.setScale(0.25, 0.25, 0.25)
     18         self.environ.setPos(-8, 42, 0)


Actually using the ipython debugger is outside the scope of this tutorial, as it is a powerful piece of software. But in a nutshell:
type ‘n’ and hit enter to execute the next line
type ‘s’ and hit enter to step into a function
type ‘r’ and hit enter to “return” or step out of a function
type ‘c’ and hit enter to continue the script (to stop stepping)
You can also execute any python statement by prefixing the line with a bang (!), i.e.

!self.environ.setHpr(90, 0, 0)

(P.S. Hit tab for command completion! You should also read up on the rest of the features of ipython and it’s powerful debugger.)

I hope this helps you debug those crazy little errors in your code!

I’ve really simplified this, and also chosen to use a easy_install instead of pip. These choices are because this is for the rank python novice. If you feel like I’m doing this the wrong way, you might be right. Do what you think best with your environment!

Thanks. This looks useful. I’ll give it a try :slight_smile: