sub-title

Also check Orama's Quora and Orama's GitHub
I shall not claim to know so much, but only that I learn new things everyday

Saturday, 25 October 2014

Python debugger has been elusive


Sometimes, when we are faced with a problem, we look for possible solutions and we may quickly resolve them. Then that same solution can be replicated to solve many other future problems of a similar nature.

That is my story about Python programming language. 


About three years ago, I had this little, but strategic, problem that required a strategic solution. I just heard from a friend that Python is very good for automation. So, I set out to read and research about Python ... and being a programmer, I was able to start using Python immediately, while I continued to build my knowledge of the language. Today, Python has become our language of choice for little, but powerful automation projects. I stress "little" for good measure.

Today, too, many of our users have expressed full satisfaction with the number of little, but powerful automation projects that we have developed using Python. For the end-users, of course, they have no idea that we use Python. In any case, it is none of their business.


The more we use Python, the more it becomes apparent that we need a fully fledged IDE for development purposes. So far, we use IDLE and PythonWin for writing our Python code. These are very basic editors, if you know what I mean; but they are the native editors for Python, so things like indentation - a key requirement for Python code - are well taken care of.


Recently, I was faced with a major hurdle in one of our numerous Python projects (one using Python 2.7 and AcGIS 10.1 - arcPy to be specific). I had been requested to make a major revision in the application, which I did using IDLE and PythonWin. Now, there were a few bugs that I needed to deal with. Unfortunately - but as expected - I was not to see any debugging tools in IDLE or PythonWin. This took me far backwards.


Again, reading and researching (of course using Google) are the key to knowledge acquisition. The dividend is that just yesterday, I just realized that Python has a debugging module called pdb (for “Python DeBugger”). Note that this is a module available in standard Python installation. This (pdb) was my savior because I was able to unearth the offending code in a matter of minutes, yet I had wasted several days before without a debugger.


To use pdb, first import the module like this:

import pdb

Then at the offending line, add the following code:

pdb.set_trace() 

You will then be able to do several standard debugging tasks. 


Listen to this: "A day that passes without learning something new is a day well wasted". I couldn't agree more.


No comments:

Post a Comment