Finding a print statement in Python

Viewed 9499

Sometimes I leave debugging printing statements in my project and it is difficult to find it. Is there any way to find out what line is printing something in particular?

Sidenote

It appears that searching smart can solve the majority of cases. In Pydev (and other IDEs) there is a Search function which allows searching through all files in a project. Of course, a similar effect can be obtained using grep with the -rn flag, although you only get line numbers instead of direct links.

"print(" works much better within my code and often there is extra text in a print statement that can be searched for with a regular expression. The most difficult case is when you have just written print(x), although this can be searched for a regular expression where the value inside x does not begin or end with quotes (thanks! BecomingGuro)

9 Answers

For Python3 i use it patched print for printing out filename, line No and function

import builtins
from inspect import getframeinfo, stack
original_print = print

def print_wrap(*args, **kwargs):
    caller = getframeinfo(stack()[1][0])
    original_print("FN:",caller.filename,"Line:", caller.lineno,"Func:", caller.function,":::", *args, **kwargs)

builtins.print = print_wrap
Related