Do you, like me, use the powerful print function to debug your Python code? Do you, like me, sometimes forget to remove lines with print and ship this out to less development-like environments? Do you, like I did, need a way to check for forgotten print statements?

I've written a dumb-simple script that scans through a directory for print statements. It uses a recursive grep command so this won't work for you hopeless Windows people :)

Download it and put it in your ~/bin (you might want to read it through) then all you have to do is to run it against a directory where there are lots of .py files:

$ ~/dev/mypythonmodule

If it doesn't work perhaps you can patch it and let me know. This might not be for the faint-hearted. There might be serious security concerns unless you have full control over your users and su account. I take no responsibility if this script rm -fr / your hd.

Here's some example output:

$ CheckoutableTemplates
---- CheckoutableTemplates/ --------
   if DEBUG:
       print "CT|",
       if type(debug_output)==StringType:
           print debug_output

---- CheckoutableTemplates/ --------
       if type(debug_output)==StringType:
           print debug_output
Laszlo Marai - 12 April 2005 [«« Reply to this]
Hopeless windows people can use CygWin. But wouldn't it be better if you used a logging library instead of pure print statements? I'm mainly a java guy (but use python every know and then) and I do know that logging is essential. And not only during development. Every softver may contain bugs, even after you start to use it. Logs help a lot then. And debug level logging will help to find the bug. There's no point in removing the log lines that you inserted during the development (well, usually). Logging should be configured from outside the software, like in java's log4j. There must be something similar for python as well.
Peter Bengtsson - 12 April 2005 [«« Reply to this]
Allow me to disagree...
...and agree.

It's a matter of lazyness. I have proper logging in some applications but the original reason for adding it was because certain methods were damn complicated with loads and loads of business-logic-conditionals. When the code isn't very complex I find it much more efficient to use print.

Of course, if I would prefer to have the logging there instead but sometimes it's just not the right moment for setting that up. The few times I use print statements are for minor things such as quick bugfixes.

Guess I should copy some of my serious logging stuff into my more matured python projects where I momentarily use print.

But I will keep using print for the little things. I reckon it will save me time in the long run.
Anonymous - 12 April 2005 [«« Reply to this]

I usually dig deeper into the toolbox for crap like this:

find . -name '*.py' | xargs egrep '^[ \t]*print'

Or, remove all print statements automatically.. sort of..

for f in $(find . -name '*.py') ;
egrep -v '^[ \t]print ' $f > $
mv $ $f
Martin - 15 April 2005 [«« Reply to this]
hi peter

grep -r -s -A 5 -B 5 <dir>

does exactly what your script does.
Peter Bengtsson - 16 April 2005 [«« Reply to this]
Wow! grep doesn't stop to impress. However my output is much nicer :)
John Machin - 16 April 2005 [«« Reply to this]
Congratulations. You have just re-invented the wheel. Unfortunately you have not re-invented the axle -- the part which pops up your favourite text editor with the cursor positioned at the start of the sought text. Anyway, who needs wheels when you've got a gyrocopters: text editors which do a "find next occurrence of pattern" across multiple files -- such things are available to hopeless *x people, aren't they?

BTW, hopeless Windows people have access to grep via GnuWin32.

Your email will never ever be published