Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a print statement in Python

Tags:

python

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)

like image 996
Casebash Avatar asked Oct 24 '09 09:10

Casebash


People also ask

What is a print statement in Python?

Python print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

Does print () have a function in Python?

Print FunctionThe Python print() function takes in any number of parameters, and prints them out on one line of text. The items are each converted to text form, separated by spaces, and there is a single '\n' at the end (the "newline" char).

What is the result of print () statement?

The PRINT statement sends data to the display terminal or to another specified print unit. Specifies that data should be output to a Spooler print unit unit#.


1 Answers

You asked about static solutions. Here's a dynamic one. Suppose you run the code and see an errant print or write to sys.stdout, and want to know where it comes from. You can replace sys.stdout and let the exception traceback help you:

>>> import sys >>> def go(): ...   sys.stdout = None ...   print "Hello!" ...  >>> go() Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "<stdin>", line 3, in go AttributeError: 'NoneType' object has no attribute 'write' >>> print "Here" Traceback (most recent call last):   File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'write' >>>  

For something a bit more sophisticated, replace 'sys.stdout' with something which reports where the print statement is located. I'll use traceback.print_stack() to show the full stack, but you can do other things like using sys._getframe() to look up one stack level in order to get the line number and filename.

import sys import traceback  class TracePrints(object):   def __init__(self):         self.stdout = sys.stdout   def write(self, s):     self.stdout.write("Writing %r\n" % s)     traceback.print_stack(file=self.stdout)  sys.stdout = TracePrints()  def a():   print "I am here"  def b():   a()  b() 

Here's the output

Writing 'I am here'   File "stdout.py", line 19, in <module>     b()   File "stdout.py", line 17, in b     a()   File "stdout.py", line 14, in a     print "I am here"   File "stdout.py", line 9, in write     traceback.print_stack(file=self.stdout) Writing '\n'   File "stdout.py", line 19, in <module>     b()   File "stdout.py", line 17, in b     a()   File "stdout.py", line 14, in a     print "I am here"   File "stdout.py", line 9, in write     traceback.print_stack(file=self.stdout) 

If you go this route, see also the 'linecache' module, which you can use to print the contents of the line. Take a look at the implementation of traceback.print_stack for details of how to do that.

like image 80
Andrew Dalke Avatar answered Oct 07 '22 00:10

Andrew Dalke