-2

Is there a way to determine which print statement produces a line of output in a python terminal? The use case is a large body of code across multiple files wherein it would be difficult or time-consuming to track down the specific line resulting in a printed statement. As a result, during runtime, I can't tell where the print statements are being executed from.

DerekG
  • 2,058
  • 1
  • 8
  • 17
  • use `grep` in a linux or `findstr` https://mkyong.com/linux/grep-for-windows-findstr-example/ – azro Oct 09 '20 at 20:26
  • Use a debugger? – OneCricketeer Oct 09 '20 at 20:28
  • Can't use grep or findstr because the output is a variable which is converted to a string rather than a string literal, so you'd need to know the variable name rather than the variable value (which is printed) to search it – DerekG Oct 09 '20 at 20:42

2 Answers2

3

My recommendation would be import logging and use that rather than using print

Then configure it to include line numbers and module names

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
0

A simple way to tell would be to just add some indicators, whether they be line numbers or the context surrounding the print statement:

print('put info here and this statement was executed from line x in file x')
...
Kevin Sheng
  • 366
  • 1
  • 4
  • 7