Find print statements in Python code

12 April 2005   6 comments   Python, Linux

Powered by Fusion×

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
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
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.

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
hi peter

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

does exactly what your script does.
Peter Bengtsson
Wow! grep doesn't stop to impress. However my output is much nicer :)
John Machin
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.
Thank you for posting a comment

Your email will never ever be published

Related posts

Your webpage in Lynx 08 April 2005
Interior Octopus 14 April 2005
Related by keywords:
Don't put title in a <link> tag 07 January 2006
Redirect stderr into becoming dots in Bash 02 September 2006
gg - wrapping git-grep 11 August 2009
Printer usability problem 24 August 2007
Grep results expanded 23 April 2005
Tip: Printer friendly pages with Page Templates in Zope 24 March 2008
Printer friendly and PDF version of every page 14 January 2004
CSS selector bug in IE? 05 December 2006
Grep in Jed 27 January 2004